【LeetCode】链表 linked list(共34题)

【2】Add Two Numbers 

 

【19】Remove Nth Node From End of List (2018年10月30日 算法群)

给了一个链表,从尾部删除第 N 个结点。

题解:two pointers,fast 先走 N 步,然后slow,fast 一起走,fast走到最后一个非空结点,slow就走到了要删除结点的前一个结点。(注意有个特判情况是如果第N个结点是头节点,那么要特殊处理)

 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 class Solution {
10 public:
11     ListNode* removeNthFromEnd(ListNode* head, int n) {
12         if (!head) {return head;}
13         ListNode *fast = head, *slow = head;
14         //1. fast goes n steps first
15         for (int k = 0; k < n && fast; ++k) {
16             fast = fast->next;
17         }
18         //2. both slow and fast move simultaneously until fast to the end of the list
19         if (fast) {
20             while (fast->next) {
21                 slow = slow->next;
22                 fast = fast->next;
23             }
24             slow->next = slow->next->next;
25         } else {
26             head = head->next;
27         }
28         return head;
29     }
30 };
View Code 

 

【21】Merge Two Sorted Lists 

合并两个有序链表变成一个大链表,大链表要求有序。(归并排序)

题解:无,直接归并

 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 class Solution {
10 public:
11     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
12         if (!l1) {return l2;}
13         if (!l2) {return l1;}
14         ListNode *p1 = l1, *p2 = l2, *head = 0, *tail = 0;
15         while (p1 && p2) {
16             if (p1->val < p2->val) {
17                 if(!head) {
18                     tail = head = p1;
19                 } else {
20                     tail = tail->next = p1;
21                 }
22                 p1 = p1->next;
23             } else {
24                 if (!head) {
25                     tail = head = p2;
26                 } else {
27                     tail = tail->next = p2;
28                 }
29                 p2 = p2->next;
30             }
31         }
32         //这里其实不用遍历了,直接连起来ok
33         if (p1) {
34             tail->next = p1;
35         }
36         if (p2) {
37             tail->next = p2;
38         }
39         return head;
40     }
41 };
View Code

 

【23】Merge k Sorted Lists

给了k个已经排好序的链表,要返回一个综合排序的大链表(归并排序)

题解:用 prioprity_queue 的运算符()重载来实现比较函数。具体运算符重载的实现方法见代码。(奇怪的是为啥pq的cmp函数要写 > ,pq里面才是从小到大排序?)

这个题目其实应该复习 priority_queue 的比较函数的实现方法。(要搞懂原理。)

 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 class Solution {
10 public:
11     //好奇怪,为啥这里要写大于符号,pq才是从小到大排序... ????
12     struct cmp{
13         bool operator() (const ListNode* node1, const ListNode* node2) {
14             return node1->val > node2->val;
15         }
16     };
17 
18     ListNode* mergeKLists(vector<ListNode*>& lists) {
19         const int n = lists.size();
20         if (n == 0) {return NULL;}
21         vector<ListNode*> ptr(n, NULL);
22         for (int i = 0; i < n; ++i) {
23             ptr[i] = lists[i];
24         }
25         ListNode *head = 0, *tail = 0;
26         priority_queue<ListNode*, vector<ListNode*>, cmp> pq;
27         for (int i = 0; i < n; ++i) {
28             if (!ptr[i]) {continue;} //注意这里有可能有的链表头节点为空,要特判
29             pq.push(ptr[i]);
30             ptr[i] = ptr[i]->next;
31         }
32         while (!pq.empty()) {
33             ListNode* node = pq.top();
34             pq.pop();
35             if (node->next) { pq.push(node->next); }
36             if (!head) {
37                 tail = head = node;
38             } else {
39                 tail = tail->next = node;
40             }
41         }
42         return head;
43     }
44 };
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 Two 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 Two 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 

猜你喜欢

转载自www.cnblogs.com/zhangwanying/p/9797184.html