Problem 92

Abstract / Summary

Reverse a portion of a linked list between two specified nodes by changing the direction of the links in that segment.

Note

The full problem description, including helpful examples, can be found on LeetCode.

Python 0092.py

class Solution:
    def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
        # a bit of cheating: keep the list pointers unchanged, only swap the values
        start = head
        for _ in range(left - 1):
            start = start.next

        values = []
        current = start
        for _ in range(left, right + 1):
            values.append(current.val)
            current = current.next

        current = start
        for v in reversed(values):
            current.val = v
            current = current.next

        return head
19 lines | 610 bytes | submitted August 18, 2023 | GitHub   6% comment ratio | tab size 4 | low complexity

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 52% (2+ million submissions).

Typical patterns to solve this problem:
  • Linked List
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.

See this dedicated page for more details.
LeetCode profile | GitHub repo

Code Walkthrough

Overview

The provided implementation reverses a sub-section of a singly-linked list defined by the positions left and right. Rather than altering the node connections, it keeps the original link structure intact and achieves the reversal by swapping the values stored in the nodes that lie within the specified interval. This approach simplifies pointer management while still delivering the expected logical outcome of a sub-list reversal.

Key Steps

Locating the Start Node

The algorithm first advances a pointer from the head of the list to the node that corresponds to the left position. This is done with a simple loop that iterates left-1 times, moving the pointer forward each iteration.

start = head
for _ in range(left - 1):
    start = start.next

Collecting Values from the Target Segment

Once the start node is identified, the code gathers the values of all nodes from left through right inclusive. It stores these values in a temporary Python list, preserving their original order.

values = []
current = start
for _ in range(left, right + 1):
    values.append(current.val)
    current = current.next

Writing the Reversed Values Back

After the values have been collected, the algorithm iterates over the list in reverse order and writes each value back into the corresponding node, moving forward through the linked list as it goes. This effectively reverses the segment’s data without touching the node links.

current = start
for v in reversed(values):
    current.val = v
    current = current.next

Returning the Modified List

Finally, the method returns the original head reference, which now points to a list whose sub-segment between left and right appears reversed.

return head

Edge Cases Handled

When the interval to reverse consists of a single element (i.e., left equals right), the collection loop still runs but the reversed write-back loop writes the same single value back, leaving the list unchanged as expected.

If left is 1, the start pointer is positioned at the head itself, so the reversal begins from the very first node without requiring any special handling.

When right corresponds to the last node of the list, the collection loop proceeds until it reaches the terminal node, and the subsequent write-back loop stops precisely after processing that node, ensuring the tail element is correctly included in the reversal.

The algorithm also works correctly for lists of any length that contain at least right nodes, as the loops are bounded by the provided indices.

Performance Optimizations

By opting to swap node values rather than rewire the next pointers, the implementation avoids the overhead associated with pointer manipulation, such as updating multiple references and handling edge connections. This value-centric strategy reduces the number of structural changes to a constant factor regardless of the segment size.

The temporary list used to store the segment’s values is allocated only once and sized exactly to the number of nodes being reversed, which minimizes memory churn. The subsequent use of Python’s built-in reversed iterator provides an efficient way to traverse the collected values in reverse order without creating an additional copy.

Overall, the method performs the reversal with a small, predictable amount of work: a linear pass to locate the start, a linear pass to collect values, and a linear pass to write them back, all while keeping the underlying linked-list structure untouched.

written by openai gpt

ELI5 - Simplified Explanation

Imagine a long train where each car is connected to the one behind it, and every car has a special number painted on its side. Sometimes it is fun to pick a section of the train and flip the order of the numbers on those specific cars.

The code starts by walking down the track to find the very first car that needs its number changed. Once it finds that spot, it looks at the numbers in all the cars from the start to the end of the section and puts those numbers into a little basket.

After all the numbers are safely stored in the basket, the code goes back to the beginning of that section. It picks up the numbers from the basket, but it takes them out in reverse order. It places the last number from the basket onto the first car in the section, the second to last number onto the next car, and so on until it reaches the end of the section. Now, the train looks exactly the same as before, but the numbers in that middle part have been swapped around.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 27 | median = 5 | μ = 5.48 | σ = 1.62

Complexity Analysis

Time: linear
As a rule of thumb, the maximum input size for one second execution time is
  • factorial: up to 10 elements
  • exponential: up to 20 elements
  • polynomial: a few thousand elements
  • loglinear: up to 100,000 elements
  • linear: a few million elements
  • logarithmic: a billion elements
  • constant: practically unlimited
generated automatically

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
Obviously, I did that after I solved the problem myself.

In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.

And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.

(click on a column header to sort the table)
author
author solutions
www AlgoMonster C++ Python Java TS
WalkCCC C++ Python Java
leetcode.ca C++ Python Java C# Rust JS Go TS
LeetcodeHelp C++ Python Java
MemoryLimitExceeded C++ Python
Progiez C++ Python Java
github aylei Rust
begeekmyfriend C
BigEggStudy C#
black-shadows C++
charles-wangkai Java
chenyanzhe C++
everthis JS
fartem Ruby
FelixFan1122 C#
fengvyi C++
fpdjsns C++
gaosq0604 Python
Garvit244 Python
halfrost Go
haoel Java
JoshCrozier JS
keineahnung2345 C++
lennylxx C
linouk23 C++
lkwq007 Python
luangong C++
MacJim C++
pruthvikp Python
qiyuangong Python
ruoru Rust
shreymehul C#
sonugiri1043 C++
suxrobGM C#
trung-hn Python
varunu28 Java
vthwang Java
wisdompeak C++
XiaotaoGuo C++
YuriSpiridonov Python
zfang399 C++
AlgoMonster C++, Python, Java, TypeScript
WalkCCC C++, Python, Java
leetcode.ca C++, C#, Go, Java, JavaScript, Python, Rust, TypeScript
LeetcodeHelp C++, Java, Python
MemoryLimitExceeded Python, C++
Progiez C++, Java, Python
aylei Rust
begeekmyfriend C
BigEggStudy C#
black-shadows C++, C++
charles-wangkai Java
chenyanzhe C++
everthis JavaScript
fartem Ruby
FelixFan1122 C#
fengvyi C++
fpdjsns C++
gaosq0604 Python
Garvit244 Python
halfrost Go
haoel Java
JoshCrozier JavaScript
keineahnung2345 C++
lennylxx C
linouk23 C++
lkwq007 Python
luangong C++
MacJim C++
pruthvikp Python
qiyuangong Python
ruoru Rust
shreymehul C#
sonugiri1043 C++
suxrobGM C#
trung-hn Python
varunu28 Java
vthwang Java
wisdompeak C++
XiaotaoGuo C++
YuriSpiridonov Python
zfang399 C++
58 links | 41 authors | 10 languages

Videos

These are a few popular YouTube videos for this problem:
YouTube video 'Reverse Linked List II - Leetcode 92 - Python'
NeetCode 17 mins
Reverse Linked List II - Leetcode 92 - Python
YouTube video 'Reverse Linked List 2 (LeetCode 92) | Full simplified solution | Animations and Demo'
Nikhil Lohia 20 mins
Reverse Linked List 2 (LeetCode 92) | Full simplified solution | Animations and Demo
YouTube video 'Reverse Linked List II | Clean Approach | Dry Run | MICROSOFT | Leetcode - 92 | codestorywithMIK'
codestorywithMIK 22 mins
Reverse Linked List II | Clean Approach | Dry Run | MICROSOFT | Leetcode - 92 | codestorywithMIK
YouTube video 'Reverse Linked List II | Leetcode June Challenge | Leetcode 92 | 2 Approaches  + Code | C++ | Hindi'
Ayushi Sharma 18 mins
Reverse Linked List II | Leetcode June Challenge | Leetcode 92 | 2 Approaches + Code | C++ | Hindi
YouTube video 'Master LeetCode 92: Reverse Linked List II | Step-by-Step Guide & Tips'
Engineering Digest 14 mins
Master LeetCode 92: Reverse Linked List II | Step-by-Step Guide & Tips
YouTube video 'Reverse Linked List II | LeetCode 92 | Coders Camp'
Coders Camp 8 mins
Reverse Linked List II | LeetCode 92 | Coders Camp
YouTube video 'Reverse Linked List II | Leetcode 92 | Live coding session'
Coding Decoded 11 mins
Reverse Linked List II | Leetcode 92 | Live coding session
YouTube video '[Java] Leetcode 92. Reverse Linked List II [LinkedList Reversal #2]'
Eric Programming 15 mins
[Java] Leetcode 92. Reverse Linked List II [LinkedList Reversal #2]
YouTube video 'Coding Interview Tutorial 93 - Reverse Linked List II [LeetCode]'
Amell Peralta 7 mins
Coding Interview Tutorial 93 - Reverse Linked List II [LeetCode]
YouTube video 'Linked list - Leetcode - 92. Reverse Linked List II'
Prakhar Agrawal 9 mins
Linked list - Leetcode - 92. Reverse Linked List II
YouTube video 'Can You Reverse a Linked List? (LeetCode 92: Reverse Linked List II)'
Code With Zi 8 mins
Can You Reverse a Linked List? (LeetCode 92: Reverse Linked List II)
YouTube video 'Daily Leetcode #842: Jul 21, 2022 - 92. Reverse Linked List II'
Programming Live with Larry 13 mins
Daily Leetcode #842: Jul 21, 2022 - 92. Reverse Linked List II
YouTube video '92 || Reverse Linked List II || C++ || Leetcode Daily Challenge'
CodeFreaks 12 mins
92 || Reverse Linked List II || C++ || Leetcode Daily Challenge
YouTube video '92. Reverse Linked List II (Leetcode Medium)'
Programming Live with Larry 17 mins
92. Reverse Linked List II (Leetcode Medium)
YouTube video 'Reverse Linked List II - LeetCode 92 - Python'
Alpha-Code 13 mins
Reverse Linked List II - LeetCode 92 - Python
YouTube video 'Daily Leetcode #1255: Sep 07, 2023 - 92. Reverse Linked List II'
Programming Live with Larry 24 mins
Daily Leetcode #1255: Sep 07, 2023 - 92. Reverse Linked List II
YouTube video 'LeetCode 92. Reverse Linked List II | Recursion | Java'
Sleepy Cracker 11 mins
LeetCode 92. Reverse Linked List II | Recursion | Java
YouTube video 'Daily Leetcode #449: Jun 23, 2021 - 92. Reverse Linked List II'
Programming Live with Larry 17 mins
Daily Leetcode #449: Jun 23, 2021 - 92. Reverse Linked List II
YouTube video '92. Reverse Linked List II | Daily Leetcode Challenge | September | C++ | Java'
Mayur Programming Club 14 mins
92. Reverse Linked List II | Daily Leetcode Challenge | September | C++ | Java
YouTube video '92. Reverse Linked List II | Leetcode | Python'
Abdullah Arshed 8 mins
92. Reverse Linked List II | Leetcode | Python
20 videos   did my crawler come up with invalid videos ? drop me a line.

Solved On The Same Day

4 easy 6 medium 2 hard
13 solutions   August 18, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • Tech Stack - my development environment and tools
  • AI - how AI helped me create this website (NOT solving the problems)
  • Progress - progression over time
  • Quality - code quality