Reverse list from the position m to n. Please use the trip to the scan is complete reversal.
Description:
. 1 ≤ ≤ m ≤ n-length list.
Example:
Input: 1-> 2-> 3-> 4- > 5-> NULL, m = 2, n = 4
Output: 1-> 4-> 3-> 2- > 5-> NULL
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/reverse-linked-list-ii
Method a: the first interpolation method
# 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 not head.next or m==n: #不先处理这些情况会慢一些
return head
dummy = ListNode(0)
dummy.next = head # 头插法
pre = dummy #dummy留着不动,用来最后返回
for _ in range(m-1):
pre = pre.next #到要处理的节点的前一个
tail = pre.next #第m个节点,处理完后是末尾的n位置节点
cur = pre.next.next
for _ in range(n-m): #反转
tmp = pre.next
tmp1 = cur.next
pre.next = cur #pre的位置是不动的,一直往pre后面插当前cur节点
cur.next = tmp
cur = tmp1
tail.next = cur #反转完后cur指向最后不用反转的部分,把它连上去
return dummy.next
Cf. example, to the back of this method is the m-1 position has been interpolated node. Because pre partial reversal has been attached to the part, so long as the final connection tail section like
The entire dummy changes:
0->1->3<->2 4->5->NULL
0-> 1-> 4-> 3 <-> 2 5-> NULL # 2.next here if not the pointer to the endless loop 5 will exceed the time limit
0->1->4->3->2->5->NULL
Here the beginning of the dummy into the most in order to avoid the case of m = 1 is less than the processing
Method two: reverse pointer
class Solution:
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
if not head or not head.next or m==n:
return head
dummy = ListNode(0)
dummy.next = head
prehead = dummy #不用反转的前面部分
for i in range(m-1):
prehead = prehead.next
cur = prehead.next #开始反转的位置
tail = prehead.next
pre = None #一个空的前向指针
for i in range(n-m+1): #这里n-m+1,才能pre指向n,cur指向n+1
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
prehead.next = pre #前连中
tail.next = cur # 连上尾
return dummy.next
Referring to examples, the inversion dummy: 0-> 1-> 2, pre: 2 <-3 <-4, cur: 5-> NULL
Even up: broken 0-> 1-> 4-> 3-> 2 between 1, 2 and then connected to 5, dummy: 0-> 1-> 4-> 3-> 2-> 5-> NULL
Here dummy plug is not in the front can not handle the case of m = 1
Written this a little faster, almost methods
# 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 m == n:
return head
dummy = ListNode(0)
dummy.next = head
a, d = dummy, dummy
for _ in range(m - 1):
a = a.next
for _ in range(n):
d = d.next
b, c = a.next, d.next
pre = b
cur = pre.next
while cur != c:
next = cur.next
cur.next = pre
pre = cur
cur = next
a.next = d
b.next = c
return dummy.next
It can also be too awkward: keep the deal with another list Jieshangqu
# 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 not head.next or m==n:
return head
res = []
while(head):
res.append(head.val)
head = head.next
lista = res[:m-1]
listb = res[m-1:n][::-1]
listc = res[n:]
lista.extend(listb)
lista.extend(listc)
cur = ListNode(0)
dummy = cur
for i in lista:
cur.next = ListNode(i)
cur = cur.next
return dummy.next