Reverse Linked List II LeetCode Solution

Here, We see Reverse Linked List II LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Reverse Linked List II LeetCode Solution

Reverse Linked List II LeetCode Solution

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]

Reverse Linked List II Leetcode Solution C++

class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int left, int right) {
       ListNode *dummy = new ListNode(0), *pre = dummy, *cur;
       dummy -> next = head;
       for (int i = 0; i < left - 1; i++) {
           pre = pre -> next;
       }
       cur = pre -> next;
       for (int i = 0; i < right - left; i++) {
           ListNode* temp = pre -> next;
           pre -> next = cur -> next;
           cur -> next = cur -> next -> next;
           pre -> next -> next = temp;
       }
       return dummy -> next;        
    }
};Code language: PHP (php)

Reverse Linked List II Leetcode Solution Java

class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
    if(head == null) return null;
    ListNode dummy = new ListNode(0); // create a dummy node to mark the head of this list
    dummy.next = head;
    ListNode pre = dummy; // make a pointer pre as a marker for the node before reversing
    for(int i = 0; i<left-1; i++) pre = pre.next;
    
    ListNode start = pre.next; // a pointer to the beginning of a sub-list that will be reversed
    ListNode then = start.next; // a pointer to a node that will be reversed

    for(int i=0; i<right-left; i++)
    {
        start.next = then.next;
        then.next = pre.next;
        pre.next = then;
        then = start.next;
    }
    return dummy.next;        
    }
}Code language: PHP (php)

Reverse Linked List II Leetcode Solution JavaScript

var reverseBetween = function(head, left, right) {
    let current = head, start = head, position = 1;
    
    while(position < left) {
        start = current
        current = current.next;
        position ++;
    }
    
    let reversedList = null,  tail = current;
    
    while(position >= left && position <= right) {
        const next = current.next;
        current.next = reversedList;
        reversedList = current;
        current = next;
        position ++
    }
    start.next = reversedList;
    tail.next = current;
    return left > 1 ? head : reversedList    
};Code language: HTML, XML (xml)

Reverse Linked List II Leetcode Solution Python

class Solution(object):
    def reverseBetween(self, head, left, right):
        if left == right:
            return head

        dummyNode = ListNode(0)
        dummyNode.next = head
        pre = dummyNode

        for i in range(left - 1):
            pre = pre.next
        
        reverse = None
        cur = pre.next
        for i in range(right - left + 1):
            next = cur.next
            cur.next = reverse
            reverse = cur
            cur = next

        pre.next.next = cur
        pre.next = reverse
        return dummyNode.next
Scroll to Top