iterate java AC


  • 0
    H
    public class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
            int count=0;
            ListNode header=new ListNode(0);
            ListNode start=null;
            ListNode end=header;
            ListNode insert=header;
            while(head!=null){
                if(count==0) start=head;
                end.next=head;
                end=end.next;
                head=head.next;
                count++;
                if(count==k){
                    count=0;
                    int cnt=0;
                    ListNode top1=start;
                    while(cnt<k){
                        ListNode tmp=start.next;
                        start.next=insert.next;
                        insert.next=start;
                        start=tmp;
                        cnt++;
                    }
                    insert=top1;
                    insert.next=null;
                    end=insert;
                }
                
            }
            return header.next;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.