Linked List
1、Partition List
[2017/2/27] 第一遍没有 BUG-FREE
[2017/3/1] 第二遍 BUG-FREE
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.
Given 1->4->3->2->5->2->null
and x = 3
,
return 1->2->2->4->3->5->null
.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition of ListNode 3 * class ListNode { 4 * public: 5 * int val; 6 * ListNode *next; 7 * ListNode(int val) { 8 * this->val = val; 9 * this->next = NULL; 10 * } 11 * } 12 */ 13 class Solution { 14 public: 15 /** 16 * @param head: The first node of linked list. 17 * @param x: an integer 18 * @return: a ListNode 19 */ 20 ListNode *partition(ListNode *head, int x) { 21 // write your code here 22 ListNode* leftDummy = new ListNode(0); 23 ListNode* rightDummy = new ListNode(0); 24 ListNode* leftTail = leftDummy; 25 ListNode* rightTail = rightDummy; 26 while (head != NULL) { 27 if (head->val < x) { 28 leftTail->next = head; 29 leftTail = head; 30 } else { 31 rightTail->next = head; 32 rightTail = head; 33 } 34 head = head->next; 35 } 36 leftTail->next = rightDummy->next; 37 rightTail->next = NULL; 38 return leftDummy->next; 39 } 40 };
2、Reverse Linked List
[2017/2/27] 第一遍 BUG-FREE
Reverse a linked list.
For linked list 1->2->3
, the reversed linked list is 3->2->1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition of ListNode 3 * 4 * class ListNode { 5 * public: 6 * int val; 7 * ListNode *next; 8 * 9 * ListNode(int val) { 10 * this->val = val; 11 * this->next = NULL; 12 * } 13 * } 14 */ 15 class Solution { 16 public: 17 /** 18 * @param head: The first node of linked list. 19 * @return: The new head of reversed linked list. 20 */ 21 ListNode *reverse(ListNode *head) { 22 // write your code here 23 if (head == NULL || head->next == NULL) { 24 return head; 25 } 26 ListNode* pre = NULL; 27 while (head != NULL) { 28 ListNode* post = head->next; 29 head->next = pre; 30 pre = head; 31 head = post; 32 } 33 return pre; 34 } 35 };
3、Remove Nth Node From End of List
[2017/3/1] 第一遍没有 BUG-FREE
Given a linked list, remove the nth node from the end of list and return its head.
Notice
The minimum number of nodes in list is n.
Given linked list: 1->2->3->4->5->null
, and n = 2
.
After removing the second node from the end, the linked list becomes 1->2->3->5->null
.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Definition of ListNode 3 * class ListNode { 4 * public: 5 * int val; 6 * ListNode *next; 7 * ListNode(int val) { 8 * this->val = val; 9 * this->next = NULL; 10 * } 11 * } 12 */ 13 class Solution { 14 public: 15 /** 16 * @param head: The first node of linked list. 17 * @param n: An integer. 18 * @return: The head of linked list. 19 */ 20 ListNode *removeNthFromEnd(ListNode *head, int n) { 21 // write your code here 22 if (n <= 0) { 23 return NULL; 24 } 25 ListNode* dummy = new ListNode(0); 26 dummy->next = head; 27 ListNode* slow = dummy; 28 ListNode* fast = dummy; 29 for (int i = 0; i < n; i++) { 30 if (fast->next == NULL) { 31 return NULL; 32 } 33 fast = fast->next; 34 } 35 while (fast->next != NULL) { 36 slow = slow->next; 37 fast = fast->next; 38 } 39 slow->next = slow->next->next; 40 return dummy->next; 41 } 42 };