JAVA AC code in a recursive way


  • -1
    F
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || k <= 1)
            return head;
        ListNode current = head, result = head, previous, next;
        for (int i = 1; i < k; i++) {
            result = result.next;
            if (result == null) {
                return head;
            }
        }
        previous = reverseKGroup(result.next, k);
        result.next = null;
        while (current != null) {
            next = current.next;
            current.next = previous;
            previous = current;
            current = next;
        }
        return result;
    }

Log in to reply
 

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