Reverse nodes in k group unique and easy approach
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
         int count = 0;
        ListNode curr = head;
        while(curr!=null&&count<k){
            count++;
            curr = curr.next;
        }
        if(count<k){
            return head;
        }
        curr = head;
        ListNode prev = null;
        ListNode tmp;
        count = 0;
        while(count<k){
            tmp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = tmp;
            count++;
        }
       head.next = reverseKGroup(curr,k);
        return prev;
    }
}
Comments (0)