Leet Code Reverse Linked List II

Leet Code Reverse Linked List II

Given this basic example, lets look at the general idea in how to reverse a linked list

next = current->next
current->next = prev;
prev = current;
current = next        

it sounds reasonable since we iterating once through the singly linked list and those operations are constant time. But, now we have a set where we need to reverse from a left nth position till the right nth position. One common solution to this problem is something like this:

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.

Constraints:

  • The number of nodes in the list is n.
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n


Basically, the solution above have 3 pointers just like the simple reverse list. it iterates nth times from left -1 to right +1 to be able to reverse the left to right and link the left -1 to right and right + 1 to left.

I asked myself the question whether is a more cleaner way in dealing with this problem.

My solution basically uses a dummy Node that that points to the head as the solution above. We then have a pointer start that points to the dummy node. The current points to the head. We introduce a nthNode counter that starts with 1. That tells the nth node position. We check if the nthNode is less than left. If is less then we set start to current. If is not less then we got our left - 1 position. We then check if current nth node is within the range of >= to left and <= to right. In that range we push the nodes to stack. We then move current to next and update nth position. Current pointer will move till is less or equal to right. When is greater than right we now have our right + 1 position.

We then use our start pointer and iterate the node stack by popping nodes and assigning it to start next pointer. After the stack is empty, we connect start next pointer to current and we return the linked list.






/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} left
 * @param {number} right
 * @return {ListNode}
 */
var reverseBetween = function (head, left, right) {

    if (!head || left === right) {
        return head;
    }

    let dummy = new ListNode(-Infinity, head)

    let start = dummy
    let current = head
    let stack = []
    let nthNode = 1

    while (current && nthNode <= right) {

        if (nthNode < left) {
            start = current
        }


        if (nthNode >= left && nthNode <= right) {
            stack.push(current);
        }

        current = current.next;
        nthNode += 1;


    }

    while (stack.length > 0) {
        start.next = stack.pop()
        start = start.next
    }

    start.next = current

    return dummy.next


};        

要查看或添加评论,请登录

Mohamed Sharif的更多文章

  • An Easier explanation for AVL Trees And Implemented Using Python

    An Easier explanation for AVL Trees And Implemented Using Python

    How To Find Parent And Children When An Array Is Given AVL trees might seem more complicated than what it seems but if…

  • Implementing A Binary Heap Using Python

    Implementing A Binary Heap Using Python

    Binary heaps are a form of binary tree but the only order that we follow is that the parent value should be either…

  • Identifying Node's Relations From Array For A Complete Binary Tree

    Identifying Node's Relations From Array For A Complete Binary Tree

    I would like to make this article more conceptual and to revise the relation of the current node value to its left…

  • Front End Development And Integration With Google API | Using Hooks, Throttle, And Dynamic Style.

    Front End Development And Integration With Google API | Using Hooks, Throttle, And Dynamic Style.

    This article can be better understood if you are an intermediate React and familiar with API Calls. In this article, I…

  • 1064. Fixed Point

    1064. Fixed Point

    https://leetcode.com/problems/fixed-point/solutions/5862817/use-binary-search-and-if-value-found-recurse-to-the-left-sin…

  • Odd Even Linked List

    Odd Even Linked List

    In this article I would like to go over a solution to solve leet code problem 328. Odd Even Linked List.

  • 1122. Relative Sort Array

    1122. Relative Sort Array

    In this problem we are given 2 arrays where the first array contains all the numbers that the second array has but the…

  • 461. Hamming Distance

    461. Hamming Distance

    In this article we are going through the Leet Code Hamming Distance problem. We are given 2 integer numbers and we are…

  • Leaf-Similar Trees

    Leaf-Similar Trees

    I will be walking through the algorithm to solve the problem of 2 nodes where we are told to find if the leaf of both…

  • Solving Leet Code 243. Shortest Word Distance

    Solving Leet Code 243. Shortest Word Distance

    In this problem, we are given a array with words and there are 2 words that already exist and the goal is to return the…

社区洞察

其他会员也浏览了