Clean java solution


  • -4
    D
    public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        int[] tmp = new int[k];
        ListNode writePointer = head;
        ListNode readPointer = head;
        int i = 0;
        while(readPointer!=null){
            tmp[i%k] = readPointer.val;
            readPointer = readPointer.next;
            if(i%k==k-1){
                for(int j=k-1;j>=0;j--){
                    writePointer.val=tmp[j];
                    writePointer = writePointer.next;
                }
            }
            i++;
        }
        return head;
    }
    

    }


Log in to reply
 

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