标签:tmp return element nts sum point ica sort list nbsp
83. Remove Duplicates from Sorted List:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* deleteDuplicates(ListNode* head) { ListNode *dup = NULL; ListNode *cur = head; if(cur == NULL || cur->next == NULL) return head; ListNode *nxt = cur->next; while(nxt != NULL){ if(cur->val == nxt->val){ ListNode *dup = nxt; nxt = nxt->next; cur->next = nxt; delete dup; }else{ cur = nxt; nxt = nxt->next; } } return head; } };
2. Add Two Numbers:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* ret, *tmp; int carry = 0, sum=0; ret = tmp = new ListNode(0); while(carry||l1||l2){ sum = carry; if(l1)sum+=l1->val; if(l2)sum+=l2->val; carry = sum/10; tmp->val = sum%10; if(l1)l1 = l1->next?l1->next:NULL; if(l2)l2 = l2->next?l2->next:NULL; if(!l1 && !l2 && !carry)return ret; tmp->next = new ListNode(0); tmp=tmp->next; } return NULL; } };
21. Merge Two Sorted Lists:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if(!l1) return l2; if(!l2) return l1; ListNode *head, *retList; if(l1->val <l2->val){ retList = head = l1; l1 = l1->next; } else{ retList = head = l2; l2 = l2->next; } while(l1 && l2){ if(l1->val <l2->val){ head->next = l1; l1 = l1->next; head = head->next; head->next = NULL; } else{ head->next = l2; l2 = l2->next; head = head->next; head->next = NULL; } } if(!l1) head->next = l2; if(!l2) head->next = l1; return retList; } };
206. Reverse Linked List:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { if(head == NULL || head->next == NULL) return head; ListNode* pre = NULL; ListNode* cur = head; ListNode* nx = cur->next; while(nx != NULL){ cur->next = pre; pre = cur; cur = nx; nx = nx->next; } cur->next = pre; return cur; } };
148. Sort List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* sortList(ListNode* head) { ListNode*p = head; ListNode*q = NULL; int temp = 0; if(head == NULL) return head; for(; p!=NULL; p=p->next) for(q=p->next; q!=NULL; q=q->next){ if(p->val > q->val){ temp = p->val; p->val = q->val; q->val = temp; } } return head; } };
19. Remove Nth Node From End of List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { if(head == NULL || n == 0) return head; ListNode* slow = head; ListNode* fast = head; ListNode* pre = head; if(fast->next == NULL) return NULL; //[1] 1 while(n>0){ //if n, slow will just point to the nth fast = fast->next; n--; } while(fast != NULL){ fast = fast->next; pre = slow; slow = slow->next; } pre->next = slow->next; //if pre==slow==head [1,2] 2 if(pre == slow) return head->next; return head; } };
141. Linked List Cycle
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCycle(ListNode *head) { ListNode* slow = head; ListNode* fast = head; while(fast && fast->next){ slow = slow->next; fast = fast->next->next; //2step if(slow == fast) return true; } return false; } };
160. Intersection of Two Linked Lists
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode* p1 = headA; ListNode* p2 = headB; while(p1 != p2){ p1 = p1?p1->next:headB; p2 = p2?p2->next:headA; } return p1; } };
203. Remove Linked List Elements
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* removeElements(ListNode* head, int val) { if(head == NULL) return head; ListNode* p = head; while (p->next != NULL){ if(p->next->val == val){ ListNode *tmp = p->next; p->next = p->next->next; delete tmp; } else{ p = p->next; } } if(head->val == val) head = head->next; return head; } };
237. Delete Node in a Linked List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void deleteNode(ListNode* node) { ListNode* p = node->next; node->val = p->val; node->next = p->next; delete p; } };
标签:tmp return element nts sum point ica sort list nbsp
原文地址:https://www.cnblogs.com/feliz/p/11130301.html