Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 92 · Reversal

Reverse Linked List II

LeetCode 92 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Detach [left, right], reverse, reconnect both ends.

ಮಧ್ಯಮLinked List
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Reverse Linked List II (LeetCode 92) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Reversal ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Cut out the sublist from position left to right, reverse it on its own, and stitch it back between the node before it and the node after it.

Approach

  1. 1

    Walk once to find the node before left, the start, the end, and the node after right.

  2. 2

    Cut the sublist off and reverse it.

  3. 3

    Link the previous node to the new front (or make it the head if left = 1).

  4. 4

    Link the old start (now the tail) to the node after.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Cut, reverse, stitchPythonO(n) time · O(1) space
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
        def reverse_list(node):
            prev = None
            temp = node
            while temp!=None:
                #prev temp front
                #.     prev temp
                front = temp.next
                temp.next = prev
                prev = temp
                temp = front
            return prev
        prev_node = None
        next_node = None
        count = 1
        node = head
        start_node = None
        last_node = None
        while node:
            if count == left-1:
                prev_node=node
            if count == left:
                start_node = node
            if count ==right:
                last_node = node
                next_node = node.next
                break
            count+=1
            node = node.next
        last_node.next = None
        reversed_node = reverse_list(start_node)
        if prev_node:
            prev_node.next = reversed_node
        else: #in case left==1
            head = reversed_node
        start_node.next = next_node
        return head
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Reverse Linked List II — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Reverse Linked List II ಹಿಂದಿನ intuition ಏನು?

Cut out the sublist from position left to right, reverse it on its own, and stitch it back between the node before it and the node after it.

Reverse Linked List II (LeetCode 92) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Walk once to find the node before left, the start, the end, and the node after right. 2. Cut the sublist off and reverse it. 3. Link the previous node to the new front (or make it the head if left = 1). 4. Link the old start (now the tail) to the node after.

Reverse Linked List II ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(n). Space: O(1). (Accepted approach: Cut, reverse, stitch.)

Reverse Linked List II ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

left = 1 has no previous node — the head changes.