题目
给定单链表的头head,起始位置from_index,结束位置to_index,反转[from_index, to_index]节点,如:
1->2->3->4->5->null, from_index=2, to_index=4,结果:
1->4->3->2->5->null
思路
先找到要反转的开始节点的前一个节点prev,结束节点的后一个end,反转该部分链表
实现
class Node():
def __init__(self, val):
self.val = val
self.next = None
# reverse [from_idnex, to_index]
def reverse_partial_list(head, from_index, to_index):
# check head
if not head:
return head
# check from_index
if from_index < 1 or from_index >= to_index:
return head
# find reverse range: (prev, end)
prev, end = None, None
cur = head
index = 0
while cur:
index += 1
if index == from_index-1:
prev = cur
if index == to_index+1:
end = cur
cur = cur.next
# check to_index and length
length = index
if to_index > length:
return head
# reverse
p = end
cur = head if not prev else prev.next
while cur != end:
next = cur.next
cur.next = p
p = cur
cur = next
if prev:
prev.next = p
return head
return p
测试
def dump_list(head):
l = []
cur = head
while cur:
l.append(cur.val)
cur = cur.next
return l
def build_list(l):
head, tail = None, None
for v in l:
node = Node(v)
if not tail:
head = node
else:
tail.next = node
tail = node
return head
def test_reverse_list(count, f, t):
print('reverse from {0} to {1}'.format(f, t))
l = []
for i in range(count):
l.append(i+1)
print(l)
head = build_list(l)
l1 = dump_list(head)
print(l1)
head = reverse_partial_list(head, f, t)
l1 = dump_list(head)
print(l1)
print('----------------------------------------')
if __name__ == '__main__':
test_reverse_list(0, 0, 0)
test_reverse_list(1, 1, 1)
test_reverse_list(2, 1, 1)
test_reverse_list(2, 1, 2)
test_reverse_list(3, 1, 2)
test_reverse_list(3, 1, 3)
test_reverse_list(3, 2, 3)
test_reverse_list(10, 1, 10)
test_reverse_list(10, 1, 5)
test_reverse_list(10, 3, 5)
test_reverse_list(10, 3, 10)
结果
➜ 5_reverse_partial_linklist python reverse_partial_linklist.py
reverse from 0 to 0
[]
[]
[]
----------------------------------------
reverse from 1 to 1
[1]
[1]
[1]
----------------------------------------
reverse from 1 to 1
[1, 2]
[1, 2]
[1, 2]
----------------------------------------
reverse from 1 to 2
[1, 2]
[1, 2]
[2, 1]
----------------------------------------
reverse from 1 to 2
[1, 2, 3]
[1, 2, 3]
[2, 1, 3]
----------------------------------------
reverse from 1 to 3
[1, 2, 3]
[1, 2, 3]
[3, 2, 1]
----------------------------------------
reverse from 2 to 3
[1, 2, 3]
[1, 2, 3]
[1, 3, 2]
----------------------------------------
reverse from 1 to 10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
----------------------------------------
reverse from 1 to 5
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[5, 4, 3, 2, 1, 6, 7, 8, 9, 10]
----------------------------------------
reverse from 3 to 5
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 5, 4, 3, 6, 7, 8, 9, 10]
----------------------------------------
reverse from 3 to 10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 10, 9, 8, 7, 6, 5, 4, 3]
----------------------------------------