my revursive java solution


  • 0
    X

    public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
    if(head==null || k==0 || k==1){
    return head;
    }
    ListNode p=head;
    ListNode q=p;
    int i=0;
    while(p!=null && i<k){
    q=p;
    p=p.next;
    i++;
    }
    if(i<k){
    return head;
    }
    ListNode temp=reverseKGroup(p,k);
    ListNode res=q;
    ListNode r=head;
    ListNode t=head.next;
    r.next=null;
    i=0;
    q=res.next;
    while(i<k-1){
    res.next=r;
    r.next=q;
    q=r;
    r=t;
    t=t.next;
    i++;
    }
    head.next=temp;
    return res;
    }
    }


Log in to reply
 

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