Chinaunix首页 | 论坛 | 博客
  • 博客访问: 254765
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-04-09 11:08:58

//Reverse Nodes in k-Group Total Accepted: 29152 Total Submissions: 115092 My Submissions Question Solution 
//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
public class ReverseNodesinkGroup {


public static void main(String[] args) {
// TODO 自动生成的方法存根


}
public ListNode reverseKGroup(ListNode head, int k) {
//k<=1
      if(k<=1)
return head;
   ListNode prehead=new ListNode(0);
   ListNode realp=prehead;
   ListNode preverse=prehead;
   ListNode p=head;
   //give head initial value
   prehead.next=head;
   while(p!=null){
    ListNode first=p;
    int count=0;
    while(count     //count ++  count and p is same
    if(p==null)
    break;
    count++;
    p=p.next;
   
    }
    // find break real condition
    if(count     break;
    preverse=reverse(first,p);
    realp.next=preverse;
    realp=first;
    realp.next=p;
   }
   return prehead.next;
   
}
private ListNode reverse(ListNode first, ListNode p) {
// TODO 自动生成的方法存根
ListNode prehead2=new ListNode(0);
ListNode newp=null;
ListNode postp=null;
ListNode rep=first;

while(rep!=p){
prehead2.next=rep;
newp=rep;
rep=rep.next;
newp.next=postp;
postp=newp;
}
return prehead2.next;
}


}

阅读(420) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~