Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL
My code:
class Solution:
def reverseBetween(self, head, m, n):
"""
:type head: ListNode
:type m: int
:type n: int
:rtype: ListNode
"""
if head==None or head.next==None:
return head
origin_head=head
count=1
list_value=[]
while head!=None:
if count>=m and count<=n:
list_value.append(head.val)
if count==m:
head1=head
count+=1
head=head.next
list_value=list_value[::-1]
count=m
while count<=n:
head1.val=list_value[count-m]
head1=head1.next
count+=1
return origin_head
Solution: https://leetcode.com/problems/reverse-linked-list-ii/