"2" Add Numbers
"19" Remove Nth Node from End of List
"21" Merge Sorted Lists
Merging two ordered lists into a large list, the large list requires order. (merge Sort)
Puzzle: None, direct merge
1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7 * };8 */9 classSolution {Ten Public: Onelistnode* mergetwolists (listnode* L1, listnode*L2) { A if(!L1) {returnL2;} - if(!L2) {returnL1;} -ListNode *p1 = L1, *p2 = L2, *head =0, *tail =0; the while(P1 &&p2) { - if(P1->val < p2->val) { - if(!head) { -Tail = head =P1; +}Else { -tail = Tail->next =P1; + } AP1 = p1->Next; at}Else { - if(!head) { -Tail = head =P2; -}Else { -tail = Tail->next =P2; - } inP2 = p2->Next; - } to } + //You don't have to go through it, just connect it straight up OK - if(p1) { theTail->next =P1; * } $ if(p2) {Panax NotoginsengTail->next =P2; - } the returnhead; + } A};
View Code
"23" Merge K Sorted Lists
Gave K a list of already ordered lists, to return a large list of integrated sorting (merge sort)
Puzzle: Use the Prioprity_queue operator () overload to implement the comparison function. the implementation of the concrete operator overloading is shown in the code. (Oddly, why does the CMP function of PQ have to be written in >, which is the small to large order in PQ?) )
In fact, this topic should review the implementation method of Priority_queue's comparison function. (To understand the principle.) )
1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7 * };8 */9 classSolution {Ten Public: One //Strange, why here to write greater than the symbol, PQ is small to large sort ...???? A structcmp{ - BOOL operator() (ConstListnode* Node1,Constlistnode*Node2) { - returnNode1->val > Node2->Val; the } - }; - -listnode* mergeklists (vector<listnode*>&lists) { + Const intn =lists.size (); - if(n = =0) {returnNULL;} +Vector<listnode*>ptr (n, NULL); A for(inti =0; I < n; ++i) { atPtr[i] =Lists[i]; - } -ListNode *head =0, *tail =0; -priority_queue<listnode*, Vector<listnode*>, cmp>PQ; - for(inti =0; I < n; ++i) { - if(!ptr[i]) {Continue;}//Note that there is a possibility that the node of the list header is empty, in Pq.push (Ptr[i]); -Ptr[i] = ptr[i]->Next; to } + while(!Pq.empty ()) { -listnode* node =pq.top (); the Pq.pop (); * if(Node->next) {Pq.push (node->next); } $ if(!head) {Panax NotoginsengTail = head =node; -}Else { thetail = Tail->next =node; + } A } the returnhead; + } -};
View Code
"24" Swap Nodes in Pairs
"25" Reverse Nodes in K-group
"61" Rotate List
"82" Remove duplicates from Sorted List II
"83" Remove duplicates from Sorted List
"86" Partition List
"92" Reverse Linked List II
"109" Convert Sorted List to Binary Search Tree
"138" Copy List with Random Pointer
"141" Linked List Cycle
"142" Linked List Cycle II
"143" Reorder List
"147" insertion Sort List
"148" Sort List
"160" intersection of Linked Lists
"203" Remove Linked List Elements
"206" Reverse Linked List
"234" palindrome Linked List
"237" Delete Node in a Linked List
"328" Odd even Linked List
"369" Plus one Linked List
"379" Design Phone Directory
"426" Convert Binary Search Tree to Sorted doubly Linked List
"430" Flatten a multilevel doubly Linked List
"445" Add Numbers II
"707" Design Linked List
"708" Insert into a Cyclic Sorted List
"725" Split Linked List in Parts
"817" Linked List components
"876" middle of the Linked List
"Leetcode" linked list (a total of 34 questions)