leetcode链表题
1. sort_list
Sort a linked list in O(n log n) time using constant space complexity.
分析:時間復雜度是nlogn,所以可以考慮歸并排序。取中點,對左邊和右邊分別遞歸排序,最后合并。
知識點:快慢指針,用來取鏈表中點;歸并排序。
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 *sortList(ListNode *head) { 12 if(!head||!head->next)return head; 13 ListNode *first = head; 14 ListNode *second = head->next; 15 while(second&&second->next){ 16 first=first->next; 17 second=second->next->next; 18 } 19 ListNode *left = sortList(first->next); 20 first->next = NULL; 21 ListNode *right = sortList(head); 22 return merge(left,right); 23 } 24 ListNode *merge(ListNode *head1, ListNode *head2) 25 { 26 if(head1 == NULL)return head2; 27 if(head2 == NULL)return head1; 28 ListNode *res , *p ; 29 if(head1->val < head2->val) 30 {res = head1; head1 = head1->next;} 31 else{res = head2; head2 = head2->next;} 32 p = res; 33 34 while(head1 != NULL && head2 != NULL) 35 { 36 if(head1->val < head2->val) 37 { 38 p->next = head1; 39 head1 = head1->next; 40 } 41 else 42 { 43 p->next = head2; 44 head2 = head2->next; 45 } 46 p = p->next; 47 } 48 if(head1 != NULL)p->next = head1; 49 else if(head2 != NULL)p->next = head2; 50 return res; 51 } 52 };2.?linked-list-cycle-ii
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
Follow up:
Can you solve it without using extra space?
分析:尋找鏈表的入環節點,也是使用快慢指針,快慢指針第一次相遇后將快指針放回鏈表頭,然后以相同的速度前進,再次相遇的節點就是鏈表的入環節點。
知識點:快慢指針找入環節點
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 *detectCycle(ListNode *head) { 12 while(!head||!head->next)return nullptr; 13 ListNode *slow = head; 14 ListNode *fast = head; 15 while(fast != NULL && fast->next != NULL){ 16 slow=slow->next; 17 fast=fast->next->next; 18 if(fast==slow)break; 19 } 20 if(!fast||!fast->next)return nullptr; 21 fast=head; 22 while(slow != fast){ 23 slow = slow->next; 24 fast = fast->next; 25 } 26 return slow; 27 } 28 };?3.?convert-sorted-list-to-binary-search-tree
? ?? Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
分析:可以遞歸建立平衡二叉查找樹,將鏈表中間的值設為樹的根,對左邊和右邊分別建立二叉查找樹。
知識點:遞歸,快慢指針
class Solution { public:TreeNode *sortedListToBST(ListNode *head) {return ToBST(head,nullptr);}TreeNode *ToBST(ListNode *head,ListNode *tail){if(head==tail)return nullptr;ListNode *fast = head;ListNode *slow = head;while(fast!=tail&&fast->next!=tail){fast=fast->next->next;slow=slow->next;}TreeNode *tr= new TreeNode(slow->val);tr->left = ToBST(head,slow);tr->right = ToBST(slow->next,tail);return tr;} };?4.?partition-list
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given1->4->3->2->5->2and x = 3,
return1->2->2->4->3->5.
?分析:將小于某值的節點按順序移至前面,我這里為了方便直接建立了兩個vector分別按序存儲值小于x和大于等于x的節點。
class Solution { public:ListNode *partition(ListNode *head, int x) {vector<int> a;vector<int> b;ListNode *li=head;while(li){if(li->val<x)a.push_back(li->val);else b.push_back(li->val);li = li->next;}li = head;for(int i=0;i<a.size();i++){li->val=a[i];li=li->next;}for(int i=0;i<b.size();i++){li->val=b[i];li=li->next;}return head;} };5.?remove-duplicates-from-sorted-list
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->2->3.?
?6.?remove-duplicates-from-sorted-list-ii
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.
分析:這里創建了一個新的頭節點,作為第一個不重復的節點;
class Solution { public:ListNode *deleteDuplicates(ListNode *head) {if((!head)||(!head->next))return head;ListNode *newHead = new ListNode(head->val-1);newHead->next = head;ListNode *last = newHead;ListNode *cur = head;while(cur&&cur->next){if(cur->val!=cur->next->val){last = cur;}else{while(cur&&cur->next&&(cur->val==cur->next->val)){cur = cur->next;}last->next = cur->next;}cur = cur->next;}return newHead->next;} };?
轉載于:https://www.cnblogs.com/xctcherry/p/8515142.html
總結
以上是生活随笔為你收集整理的leetcode链表题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: msp430入门学习35
- 下一篇: day11_用例执行顺序、跳过用例ski