[
linked list
]
BinarySearch 0435 Reverse an Inner Linked List
Problem statement
https://binarysearch.com/problems/Reverse-an-Inner-Linked-List/
Solution
Equal to Leetcode 0092. Reverse Linked List II.
Complexity
It is O(n)
for time and O(1)
for space.
Code
class Solution:
def solve(self, head, m, n):
m, n = m + 1, n + 1
if m == n: return head
dummy = LLNode(0)
dummy.next = head
pre = dummy
for i in range(m-1):
pre = pre.next
curr = pre.next
nxt = curr.next
for i in range(n-m):
tmp = nxt.next
nxt.next = curr
curr = nxt
nxt = tmp
pre.next.next = nxt
pre.next = curr
return dummy.next