1. 程式人生 > >Reverse Nodes in k-Group

Reverse Nodes in k-Group

rem bsp val lte direct then pos alter prev

【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