[Leetcode Series] [] [medium] inversion algorithm list II

topic:

Topic links:  https://leetcode-cn.com/problems/reverse-linked-list-ii/

 

Problem-solving ideas:

Method One: an array of new list +

Since the title is not required to use the original node, so only need to exchange the corresponding node values ​​on the line

Therefore, a relatively simple approach is to value in all nodes, into an array, the corresponding values ​​are exchanged, then generate a new list

A method for exchanging an array of values:

  1. M and n are switched corresponding to the value of the subscript
  2. m to the left, to the right to continue the exchange value, n, until m> = n up

Illustrated as follows:

 

Method Two: Recursive

If recursive, in fact, a similar method may be implemented in a one-way function list illustrated process is as follows:

variable:

  1. left: left to be exchanged node values, global variables
  2. right: the right to be exchanged node, recursion variable values
  3. stop: whether to stop recursion, global variables

Logic illustrated as follows:

At the beginning, each recursive, if left to move to the start node if not, moves to the right each time left 1; right moves to end node if not, a rightward movement

At this time, the left and right stops moving, the exchange value of the left and right, and a global variable left is moved rightward, back to the previous recursion

Because the right is a recursive variable, and just only the exchange value, and did not modify the order of the node

Therefore, when the return to the previous recursion, to achieve a similar function prev doubly-linked list, so that right before a fallback node, as shown below:

At this time, the exchange value again, and move to the right left, back to the previous recursion:

At this time, the left and right found exchange point to the same node, modify the global variable stop to True, the recursion stops all upper values ​​of the exchange operation, the completion value

 

Code:

method one:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
        val_lst = []
        while head:
            val_lst.append(head.val)
            head = head.next
            
        m -= 1
        n -= 1
        while m < n:
            val_lst[m], val_lst[n] = val_lst[n], val_lst[m]
            m += 1
            n -= 1
            
        dummy = ListNode(0)
        new_head = dummy
        for val in val_lst:
            new_head.next = ListNode(val)
            new_head = new_head.next
            
        return dummy.next

 

Method Two:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
        if not head or m >= n:
            return head
        
        left, right = head, head
        stop = False
        
        def reverse_help(right, m, n):
            nonlocal left, stop
            
            if n <= 1:
                return
            
            right = right.next
            
            if m > 1:
                left = left.next
                
            reverse_help(right, m - 1, n - 1)
            
            if not stop:
                left.val, right.val = right.val, left.val
                left = left.next

            if left is right or left.next is right:
                stop = True

        reverse_help(right, m, n)
        return head

 

Published 100 original articles · won praise 4 · Views 1469

Guess you like

Origin blog.csdn.net/songyuwen0808/article/details/105319300