# python链表反转(递归)

class ListNode():
def __init__(self,x):
self.val =x
self.next = None

while curr is not None:
print(curr.val,end=' -> ')
curr=curr.next
print("None")

return
prev = None
while curr is not None:
temp = curr.next
curr.next = prev
prev = curr
curr = temp
return prev

def reverse(root, prep, k):
curr = root
prev = None
temp = None
while curr is not None and k>0:
temp = curr.next
curr.next = prev
prev = curr
curr= temp
k -=1
root.next= temp
prep.next= prev
return prev
dummy = ListNode(-1)
k = 0
p = dummy
start = None
while p is not None:
if k==m:
start = p
if k==n+1:
reverse(start.next, start, n-m+1)
return dummy.next
k +=1
p=p.next

def reverse(root, prep, k):
curr = root
prev = None
temp = None
while curr is not None and k>0:
temp = curr.next
curr.next = prev
prev = curr
curr= temp
k -=1
root.next= temp
prep.next= prev
return root
if start_pos <1:
raise ValueError("Start position should be greater than 0")
dummy = ListNode(-1)
k = 0
p = dummy
start = None
while p is not None:
if k == start_pos:
start = p
if k > 1 and k%n==1:
start = reverse(start.next, start, n)
if not repeat:
return dummy.next
k +=1
p=p.next
return dummy.next

#printNode(result)
return result

n=10
l0=ListNode(0)
for i in range(1,n):
exec(f"l{i}=ListNode({i})")
exec(f"l{i-1}.next=l{i}")

printNode(l0)
printNode(reverse(l0))

10-08

09-16 4899
07-19 2414
08-20 1918
06-11 1661
07-05 2万+
06-26 5344
12-11 1万+
01-26 6214
04-04 1万+
05-15 6088
07-16 1万+