class LNode:
def __init__(self, val):
self.val = val
self.next = None
def reverseList(Phead):
pre, cur = None, Phead
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre
if __name__ == "__main__":
Phead = LNode(1)
p1 = LNode(2)
p2 = LNode(3)
p3 = LNode(4)
p4 = LNode(5)
p5 = LNode(6)
Phead.next = p1
p1.next = p2
p2.next = p3
p3.next = p4
p4.next = p5
L = reverseList(Phead)
while L is not None:
print(L.val)
L = L.next
反转链表
最新推荐文章于 2025-03-12 20:11:42 发布