Java 8 ms solution


  • 0

    '''
    public ListNode reverseKGroup(ListNode head, int k) {
    ListNode temp=head;
    int len=0;
    while(temp!=null){
    len++;
    temp=temp.next;
    }
    return function(head,k,len);
    }

    public ListNode function(ListNode head, int k,int len){
    if(len<k || head==null)
    return head;
    ListNode cur=head;
    ListNode next=null;
    ListNode prev=null;
    int i=0;
    while(i<k &&cur!=null)
    {
    next=cur.next;
    cur.next=prev;
    prev=cur;
    cur=next;
    i++;
    }
    head.next=function(cur,k,len-k);
    return prev;
    }

    '''


Log in to reply
 

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