25. Reverse Nodes in k-Group

Problem:

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

Example:

Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

Note:

Solution:

  1. Find the end node of a portion that needs to be reversed.
  2. Get the next node of the end node.
  3. Reverse the portion using the next node as edge(null) pointer.
  4. Connect it back to the main linked list.

    /**

    return prehead.next };

    function reverseLinkList (head, nullNode = null) { let prev = nullNode let curr = head while (curr !== nullNode) { const next = curr.next curr.next = prev prev = curr curr = next } return prev };

Template generated via Leetmark.