【LeetCode】25. Reverse Nodes in k-Group (2 solutions)
2014-12-19 16:02
281 查看
Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list:
For k = 2, you should return:
For k = 3, you should return:
解法一:
看到逆序,第一反应就是栈。
使用栈,每k个结点进栈,再出栈,就实现了逆序。
![](http://images.cnitblog.com/blog/458814/201502/141518166838052.jpg)
解法二:
自定义函数reverse(begin, end)
对[begin, end]范围内实现逆序,并且更新begin, end
逐k次调用即可。
注意:
(1)由于需要更新begin, end,因此参数形式为ListNode*&
(2)在[begin,end]范围内实现逆序之后,需要链如原先的链表,不可脱离
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list:
1->2->3->4->5
For k = 2, you should return:
2->1->4->3->5
For k = 3, you should return:
3->2->1->4->5
解法一:
看到逆序,第一反应就是栈。
使用栈,每k个结点进栈,再出栈,就实现了逆序。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *reverseKGroup(ListNode *head, int k) { ListNode* newhead = new ListNode(-1); ListNode* tail = newhead; ListNode* begin = head; ListNode* end = begin; while(true) { int count = k; while(count && end != NULL) { end = end->next; count --; } if(count == 0) {//reverse from [begin, end) stack<ListNode*> s; while(begin != end) { s.push(begin); begin = begin->next; } while(!s.empty()) { ListNode* top = s.top(); s.pop(); tail->next = top; tail = tail->next; } } else {//leave out tail->next = begin; break; } } return newhead->next; } };
![](http://images.cnitblog.com/blog/458814/201502/141518166838052.jpg)
解法二:
自定义函数reverse(begin, end)
对[begin, end]范围内实现逆序,并且更新begin, end
逐k次调用即可。
注意:
(1)由于需要更新begin, end,因此参数形式为ListNode*&
(2)在[begin,end]范围内实现逆序之后,需要链如原先的链表,不可脱离
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *reverseKGroup(ListNode *head, int k) { if(head == NULL) return NULL; if(k == 1) //no swap return head; int i = 1; //head node ListNode* newhead = new ListNode(-1); newhead->next = head; ListNode* tail = newhead; ListNode* begin = head; ListNode* end = begin; while(end != NULL) { if(i%k == 0) { reverse(begin, end); tail->next = begin; tail = end; //new begin begin = end->next; } end = end->next; i ++; } return newhead->next; } void reverse(ListNode*& begin, ListNode*& end) {//reverse the list. begin points to new begin, end points to new end if(begin == end) {//only one node return; } else if(begin->next == end) {//two nodes begin->next = end->next; end->next = begin; //swap begin and end ListNode* temp = begin; begin = end; end = temp; } else {//at least three nodes ListNode* pre = begin; ListNode* cur = pre->next; ListNode* post = cur->next; while(post != end->next) { cur->next = pre; pre = cur; cur = post; post = post->next; } cur->next = pre; //old begin points to the new end end = begin; end->next = post; //cur points to the old end begin = cur; } } };
![](http://images.cnitblog.com/blog/458814/201502/141516145111343.jpg)
相关文章推荐
- [LeetCode] Reverse Nodes in K-Group
- leetcode:Reverse Nodes in k-Group(以k为循环节反转链表)【面试算法题】
- LeetCode_OJ【25】Reverse Nodes in k-Group
- python写算法题:leetcode: 25. Reverse Nodes in k-Group
- leetcode_c++:链表:Reverse Nodes in k-Group(025)
- LeetCode——Reverse Nodes in k-Group
- LeetCode-25. Reverse Nodes in k-Group
- leetcode 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group
- 【LeetCode】Swap Nodes in Pairs & Reverse Nodes in k-Group
- leetcode 25. Reverse Nodes in k-Group
- leetcode 25. Reverse Nodes in k-Group
- leetcode-Group Anagrams
- leetcode之Reverse Nodes in k-Group
- leetcode Group Anagrams
- leetcode 25. Reverse Nodes in k-Group & leetcode 92. Reverse Linked List II
- LeetCode 25. Reverse Nodes in k-Group
- LeetCode25. Reverse Nodes in k-Group
- LeetCode 25 - Reverse Nodes in k-Group
- [LeetCode] Reverse Nodes in k-Group
- 【LeetCode】96. Unique Binary Search Trees (2 solutions)