Advertisement

Reverse Linked List II - LeetCode 92 Solution

Reverse Linked List II - Complete Solution Guide

Reverse Linked List II is LeetCode problem 92, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the head of a singly linked list and two integers left and right where left <= right , reverse the nodes of the list from position left to position right , and return the reversed list . Example 1: Input: head = [1,2,3,4,5], left = 2, right = 4 Output: [1,4,3,2,5] Example 2: Input: head = [5], left = 1, right = 1 Output: [5] Constraints: The number of nodes in the list is n . 1 <= n <= 500 -500 <= Node.val <= 500 1 <= left <= right <= n Follow up: Could you do it in one pass?

Detailed Explanation

The problem requires reversing a specific portion of a singly linked list, defined by the 'left' and 'right' indices (1-indexed). Given the head of the linked list and the left and right boundaries, the goal is to reverse the nodes from the 'left' position up to and including the 'right' position. The reversed sublist should then be re-integrated into the original linked list. The constraints specify that the list size is between 1 and 500, node values between -500 and 500, and left/right are valid indices within the list's boundaries.

Solution Approach

The solution uses an iterative approach to reverse the specified portion of the linked list. A dummy node is used to handle the case where 'left' is 1. The algorithm first locates the node immediately before the 'left' position. Then, it iteratively reverses the nodes from 'left' to 'right'. Finally, it reconnects the reversed sublist with the rest of the linked list.

Step-by-Step Algorithm

  1. Step 1: Create a dummy node pointing to the head of the linked list. This simplifies the logic when 'left' is 1.
  2. Step 2: Traverse the linked list to find the node immediately before the 'left' position. This node is stored as 'pre_left'.
  3. Step 3: Identify the starting node of the section to be reversed, which is 'pre_left.next', and store it as 'start_node'.
  4. Step 4: Initialize 'prev_node' to null, 'current_node' to 'start_node'.
  5. Step 5: Iterate 'right - left + 1' times to reverse the sublist: store the next node, reverse the 'next' pointer of the current node to point to the previous node, move 'prev_node' to the current node, and move 'current_node' to the next node.
  6. Step 6: After reversing the sublist, reconnect it to the original list: set 'pre_left.next' to 'prev_node' (the new head of the reversed sublist) and set 'start_node.next' to 'current_node' (the node immediately after the reversed sublist).
  7. Step 7: Return 'dummy.next', which is the head of the modified linked list.

Key Insights

  • Insight 1: Using a dummy node simplifies the process of handling the head node and avoids special case logic when 'left' is 1.
  • Insight 2: Iterative reversal is efficient and avoids recursion's overhead.
  • Insight 3: Keeping track of the node immediately before the 'left' position and the node at the 'left' position before reversal allows for easy reconnection of the reversed sublist.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Linked List.

Companies

Asked at: Adobe, Amazon, Apple, Arista Networks, Bloomberg, Disney, EPAM Systems, Meta, Microsoft, Nutanix, Nvidia, Revolut, TikTok, Uber, Yahoo, Zoho.