赞
踩
- struct ListNode* removeElements(struct ListNode* head, int val){
- struct ListNode* cur = head;
- struct ListNode* tail = NULL;
- while (cur)
- {
- if (cur->val == val)
- {
- struct ListNode* next = cur->next;
- if (tail == NULL)
- {
- head = next;
- }
- else
- {
- tail->next = cur->next;
- }
- free(cur);
- cur = next;
- }
- else
- {
- tail = cur;
- cur = cur->next;
- }
- }
- return head;
- }
- struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
- struct ListNode* slow = pListHead;
- struct ListNode* fast = slow;
- while(k--)
- {
- if(fast)
- fast = fast->next;
- else
- return NULL;
- }
-
- while(fast)
- {
- slow = slow->next;
- fast = fast->next;
- }
-
- return slow;
- }
- class Partition {
- public:
- ListNode* partition(ListNode* pHead, int x) {
- ListNode* headless = (ListNode*)malloc(sizeof(ListNode));
- ListNode* headgreater = (ListNode*)malloc(sizeof(ListNode));
- headless->next = headgreater->next = NULL;
- ListNode* curless = headless;
- ListNode* curgreater = headgreater;
- ListNode* cur = pHead;
- while(cur)
- {
- if(cur->val < x)
- {
- curless->next = cur;
- curless = curless->next;
- }
- else
- {
- curgreater->next = cur;
- curgreater = curgreater->next;
- }
- cur = cur->next;
- }
- curgreater->next = NULL;
- curless->next = headgreater->next;
- pHead = headless->next;
- free(headless);
- free(headgreater);
- return pHead;
- }
- };
- class PalindromeList {
- public:
- bool chkPalindrome(ListNode* A) {
- ListNode* slow = A;
- ListNode* fast = A;
- while(fast && fast->next)
- {
- slow = slow->next;
- fast = fast->next->next;
- }
- ListNode* prev = NULL;
- ListNode* cur = slow;
- while(cur)
- {
- ListNode* next = cur->next;
- cur->next = prev;
- prev = cur;
- cur = next;
- }
- while(prev)
- {
- if(A->val == prev->val)
- {
- A = A->next;
- prev = prev->next;
- }
- else
- {
- return false;
- }
- }
- return true;
-
- }
- };
- struct ListNode* reverseList(struct ListNode* head){
- struct ListNode* prev = NULL;
- struct ListNode* cur = head;
- while(cur)
- {
- struct ListNode* next = cur->next;
- cur->next = prev;
- prev = cur;
- cur = next;
- }
- return prev;
- }
- struct ListNode* middleNode(struct ListNode* head){
- struct ListNode* prev = head;
- struct ListNode* cur = head;
- while(cur && cur->next)
- {
- prev = prev->next;
- cur = cur->next->next;
- }
- return prev;
- }
- struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
- struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
- head->next = NULL;
- struct ListNode* tail = head;
- struct ListNode* curA = list1;
- struct ListNode* curB = list2;
- while(curA && curB)
- {
- if(curA->val < curB->val)
- {
- tail->next = curA;
- curA = curA->next;
- }
- else
- {
- tail->next = curB;
- curB = curB->next;
- }
- tail = tail->next;
- }
- if(curA == NULL)
- {
- tail->next = curB;
- }
- else
- {
- tail->next = curA;
- }
- struct ListNode* Head = head->next;
- free(head);
- return Head;
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。