首页 > 代码库 > LeetCode Reverse Nodes in k-Group

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 /**  2  * Definition for singly-linked list.  3  * public class ListNode {  4  *     int val;  5  *     ListNode next;  6  *     ListNode(int x) {  7  *         val = x;  8  *         next = null;  9  *     } 10  * } 11  */ 12 public class Solution { 13     public ListNode reverseKGroup(ListNode head, int k) { 14  15         if (head==null) { 16             return null; 17         } 18         if (k == 1) { 19             return head; 20         } 21  22         ListNode temp=head; 23         ListNode doRe = head, nextList = null, first = null, last = null; 24         int i=1; 25  26         while (temp != null) { 27  28             if (i % k == 0) { 29                 if (i == k) { 30                     first = reverseBetween(doRe, i / k, i); 31  32                 } else { 33                     last.next = reverseBetween(doRe, 1, k); 34                 } 35                 last = doRe; 36                 temp = doRe.next; 37                 doRe = doRe.next; 38             } else { 39             //    last = temp; 40                 temp = temp.next; 41             } 42  43             ++i; 44         } 45         return first==null?doRe:first; 46  47     } 48      49      50     public ListNode reverseBetween(ListNode head, int m, int n) { 51         if (head==null) { 52             return null; 53         } 54         if(n==m){ 55             return head; 56         } 57         ListNode temp=head,doRe = null,second=null,first=null,last=null; 58         int i=1; 59  60         while (temp!=null) { 61             if (i==m-1) { 62                 first=temp; 63             } 64             if (i==m) { 65                 doRe=temp; 66             } 67             if (i==n) { 68                 last=temp; 69                 second=temp.next; 70                 if(first!=null) first.next=last; 71                 last.next=null; 72  73                 break; 74             } 75             ++i; 76             temp=temp.next; 77         } 78         reverse(doRe); 79         doRe.next=second;     80  81         if (m>1) { 82             return head; 83         }else { 84             return last; 85         } 86  87     } 88     private void reverse(ListNode head) { 89         ListNode pre = null,curr,next; 90  91         curr=head; 92         if (head==null) { 93             return; 94         } 95         while (curr!=null) { 96             next=curr.next; 97             curr.next=pre; 98             pre=curr; 99             curr=next;100         }101     }102 103 104 105 }

 

LeetCode Reverse Nodes in k-Group