大牛们帮忙,Rverse Nodes in k-Group# JobHunting - 待字闺中
h*o
1 楼
Rverse Nodes in k-Group:
看了好久,没发现问题在哪儿,求大牛们过目~~
谢了!
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
// Start typing your Java solution below
// DO NOT write main() function
if(head==null)
return null;
ListNode begin=head;
ListNode pre=null;
ListNode end=null;
while(begin!=null){
ListNode cur=begin;
for(int i=0;i cur=cur.next;
if(cur==null)
return head;
}
end=cur;
ListNode temp=cur.next;
if(pre!=null){
pre.next=end;
}
else
head=end;
ListNode connect= reverseLinkedList(begin, end);
pre=connect;
begin=temp;
}
return head;
}
private ListNode reverseLinkedList(ListNode begin, ListNode end){
ListNode pre=null;
ListNode cur=begin;
while(cur!=end){
ListNode next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
cur.next=pre;
return begin;
}
}
看了好久,没发现问题在哪儿,求大牛们过目~~
谢了!
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
// Start typing your Java solution below
// DO NOT write main() function
if(head==null)
return null;
ListNode begin=head;
ListNode pre=null;
ListNode end=null;
while(begin!=null){
ListNode cur=begin;
for(int i=0;i
if(cur==null)
return head;
}
end=cur;
ListNode temp=cur.next;
if(pre!=null){
pre.next=end;
}
else
head=end;
ListNode connect= reverseLinkedList(begin, end);
pre=connect;
begin=temp;
}
return head;
}
private ListNode reverseLinkedList(ListNode begin, ListNode end){
ListNode pre=null;
ListNode cur=begin;
while(cur!=end){
ListNode next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
cur.next=pre;
return begin;
}
}