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

Reverse Nodes in k-Group

时间:2017-08-30 23:35:32      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:tco   current   integer   link   not   hang   linked   while   leetcode   

【LeetCode】

题目描述:

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked 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 个结点之后要注意这 k 个结点的头尾和谁相连。

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 class Solution {
10     public ListNode reverseKGroup(ListNode head, int k) {
11         if(head == null || head.next == null || k < 2)
12             return head;
13         ListNode dummy = new ListNode(0);
14         dummy.next = head;
15         ListNode begin = dummy;
16         int i = 0;
17         while(head != null){
18             i++;
19             if(i % k == 0){
20                 begin = reverse(begin, head.next);
21                 head = begin.next;
22             }else
23                 head = head.next;
24         }
25         return dummy.next;
26     }
27     public ListNode reverse(ListNode begin, ListNode end){ // 反转(begin, end)开区间之间的 k 个结点
28         ListNode prev = begin;
29         ListNode curr = begin.next;
30         ListNode first = curr;
31         while(curr != end){
32             ListNode next = curr.next;
33             curr.next = prev;
34             prev = curr;
35             curr = next;
36         }
37         begin.next = prev;
38         first.next = curr;
39         return first;
40     }
41 }

讨论区别人的递归版解法:还带注释的:

 1 public ListNode reverseKGroup(ListNode head, int k) {
 2     ListNode curr = head;
 3     int count = 0;
 4     while (curr != null && count != k) { // find the k+1 node
 5         curr = curr.next;
 6         count++;
 7     }
 8     if (count == k) { // if k+1 node is found
 9         curr = reverseKGroup(curr, k); // reverse list with k+1 node as head
10         // head - head-pointer to direct part, 
11         // curr - head-pointer to reversed part;
12         while (count-- > 0) { // reverse current k-group: 
13             ListNode tmp = head.next; // tmp - next head in direct part
14             head.next = curr; // preappending "direct" head to the reversed list 
15             curr = head; // move head of reversed part to a new node
16             head = tmp; // move "direct" head to the next node in direct part
17         }
18         head = curr;
19     }
20     return head;
21 }

 讨论区里别人的非递归版解法,16行,给跪了。。。

 1 public ListNode reverseKGroup(ListNode head, int k) {
 2     int n = 0;
 3     for (ListNode i = head; i != null; n++, i = i.next);
 4     
 5     ListNode dmy = new ListNode(0);
 6     dmy.next = head;
 7     for(ListNode prev = dmy, tail = head; n >= k; n -= k) {
 8         for (int i = 1; i < k; i++) {
 9             ListNode next = tail.next.next;
10             tail.next.next = prev.next;
11             prev.next = tail.next;
12             tail.next = next;
13         }
14         prev = tail;
15         tail = tail.next;
16     }
17     return dmy.next;
18 }

 

Reverse Nodes in k-Group

标签:tco   current   integer   link   not   hang   linked   while   leetcode   

原文地址:http://www.cnblogs.com/niuxichuan/p/7455553.html

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