数据结构之链表反转

class Solution1(object):
    def __init__(self,x):
        self.val = x
        self.next = None
        
def reverse_list(head):
        cur, pre = head, None
        while cur:
            cur.next, pre, cur = pre, cur, cur.next
        return pre
head = Solution1(1)
p1 = Solution1(2)   # 建立链表 1 >> 2 >> 3 >> 4 >> 5 
p2 = Solution1(3)
p3 = Solution1(4)
p4 = Solution1(5)
head.next = p1
p1.next = p2
p2.next = p3
p3.next = p4
p = reverse_list(head)
while p:
    print(p.val)  # 5 >> 4 >> 3 >> 2 >> 1
    p = p.next

 

posted @ 2018-10-21 17:43  PLAY_JOY  阅读(294)  评论(0编辑  收藏  举报