# 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))


08-03 8284

02-03 2523

06-26 4251

03-21 5416

07-16 1万+

06-06

12-30 726

12-01 1741

12-11 1万+

11-28 2528

#### 逆序反转单链表 Python 版

©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。