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

LeetCode Reverse Nodes in k-Group

时间:2014-12-20 01:59:41      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/birdhack/p/4174950.html

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