LeetCode: 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: 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
 
解答:
1. 用递归实现,逐层进行反转。遇到最后一个如果个数不为k,再反转一次即可。
 1 // Solution 1:
 2     // recursion.
 3     // 思路是先反转,如果发现长度不够K,再翻回来
 4     public ListNode reverseKGroup1(ListNode head, int k) {
 5         if (head == null) {
 6             return null;
 7         }
 8         
 9         return rec(head, k);
10     }
11     
12     public ListNode rec(ListNode head, int k) {
13         ListNode dummy = new ListNode(0);
14         
15         dummy.next = head;
16         
17         int len = 0;
18         ListNode cur = head;
19         while (cur != null) {
20             ListNode tmp = cur.next;
21             cur.next = dummy.next;
22             dummy.next = cur;
23             cur = tmp;
24             len++;
25             
26             // reverse all the k nodes.
27             if (len == k) {
28                 // link the tail to the next node in the list.
29                 head.next = rec(tmp, k);
30                 break;
31             }
32         }
33         
34         if (len != k) {
35             ListNode node = dummy.next;
36             dummy.next = null;
37             
38             while (node != null) {
39                 ListNode next = node.next;
40                 node.next = dummy.next;
41                 dummy.next = node;
42                 node = next;
43             }
44         }
45         
46         return dummy.next;
47     }
View Code

2. 使用一个专用的反转函数来进行反转,从头到尾遍历,遍历到K的时候,使用Pre-Next指针的方式进行反转。这个方法比递归更棒。

 1 /*
 2      * Solution 2:
 3      * 使用区间反转的办法, iteration.
 4      * */
 5     public ListNode reverseKGroup(ListNode head, int k) {
 6         if (head == null) {
 7             return null;
 8         }
 9         
10         ListNode dummy = new ListNode(0);
11         dummy.next = head;
12         
13         ListNode cur = head;
14         ListNode pre = dummy;
15         
16         int cnt = 0;
17         
18         while (cur != null) {
19             cnt++;
20             if (cnt == k) {
21                 cnt = 0;
22                 pre = reverse(pre, cur.next);
23             }
24             cur = cur.next;
25         }
26         
27         return dummy.next;
28     }
29     
30     /**
31      * Reverse a link list between pre and next exclusively
32      * an example:
33      * a linked list:
34      * 0->1->2->3->4->5->6
35      * |           |   
36      * pre        next
37      * after call pre = reverse(pre, next)
38      * 
39      * 0->3->2->1->4->5->6
40      *          |  |
41      *          pre next
42      * @param pre 
43      * @param next
44      * @return the reversed list's last node, which is the precedence of parameter next
45      */
46     private static ListNode reverse(ListNode pre, ListNode next){
47         ListNode cur = pre.next;
48         
49         // record the new tail.
50         ListNode last = cur;
51         while (cur != next) {
52             ListNode tmp = cur.next;
53             cur.next = pre.next;
54             pre.next = cur;
55             cur = tmp;
56         }
57         
58         last.next = next;
59         return last;
60     }

主页君的GitHub代码

ref: http://www.cnblogs.com/lichen782/p/leetcode_Reverse_Nodes_in_kGroup.html

posted on 2014-10-20 11:13  Yu's Garden  阅读(238)  评论(0编辑  收藏  举报

导航