码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】linked list(共34题)

时间:2018-10-16 13:41:14      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:pair   else   有序链表   方法   tree   get   design   dir   apple   

【2】Add Two Numbers 

【19】Remove Nth Node From End of List 

 

【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 

【LeetCode】linked list(共34题)

标签:pair   else   有序链表   方法   tree   get   design   dir   apple   

原文地址:https://www.cnblogs.com/zhangwanying/p/9797184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!