Similar Problems

Similar Problems not available

Reverse Nodes In Even Length Groups - Leetcode Solution

Companies:

LeetCode:  Reverse Nodes In Even Length Groups Leetcode Solution

Difficulty: Medium

Topics: linked-list  

Problem Description:

Given a linked list, reverse the nodes of each even-indexed group and leave the odd-indexed groups as is.

Example:

Input: 1->2->3->4->5->6->7->8 Output: 1->4->3->2->5->8->7->6

Explanation: The even-indexed groups are (1, 2), (3, 4), (5, 6), and (7, 8). We swap the nodes within each group to get the result.

Solution:

First, we create a helper function that can reverse the nodes of a linked list given the head and tail nodes. We use this helper function to reverse the nodes in each even-indexed group.

Next, we traverse the linked list, keeping track of the even-indexed nodes (indexed starting at 0). When we reach the end of an even-indexed group, we call the helper function to reverse the nodes within that group. We repeat this process until we reach the end of the list.

Algorithm:

  1. Define a function to reverse the nodes of a linked list given the head and tail nodes. The function should return the new head and tail nodes.

  2. Define a function to reverse nodes in the linked list at even indices. This functions walks to the 2n+1th Node starting from the beginning of the list.

  3. Define a new function that walks to the end of each even-indexed group and reverses the nodes within that group using the reverse function defined in step 1.

  4. Traverse the linked list, keeping track of the even-indexed nodes (indexed starting at 0). When we reach the end of an even-indexed group (i.e. the (2n+1)th node, assuming 0-indexing), we reverse the nodes within that group using the helper function.

  5. Return the new head of the modified linked list.

Pseudocode:

// Helper function to reverse the nodes of a linked list given the head and tail nodes function reverse(head, tail): prev = null current = head while (prev != tail): next = current.next current.next = prev prev = current current = next return (tail, head)

// Function to reverse nodes in even-indexed groups function reverseEvenGroups(head): if (head == null): return null even = head.next if (even == null): return head odd = head while (even != null): (even, odd).next = (odd, even).next odd.next = even.next even.next = odd even = odd.next if (even == null): break odd = odd.next even = even.next return head

// Main function to reverse nodes in even-indexed groups function reverseNodesInEvenLengthGroups(head): if (head == null): return null even = head.next if (even == null): return head odd = head while (even != null): odd.next = reverseEvenGroups(odd.next) odd = even even = even.next if (even == null): break odd = odd.next even = even.next return head

Time complexity:

The time complexity of this algorithm is O(n), where n is the number of nodes in the linked list. We traverse the linked list once, and for each even-indexed group, we reverse the nodes in that group using the reverse function, which takes O(k) time, where k is the number of nodes in the group (which is at most n/2, since we only consider even-indexed groups). Therefore, the total time complexity of the algorithm is O(n).

Reverse Nodes In Even Length Groups Solution Code

1