牛客网2:反转链表

输入一个链表,反转链表后,输出新链表的表头。

class ListNode:
    def __init__(self,x)
        self.val = x
        self.next = None
class solution:
    def reverse(pHead):
        if not pHead or not pHead.next:
            return pHead
        last = None
        while pHead:
            tmp = pHead.next
            pHead.next = last
            last=pHead
            pHead=tmp
        return last

猜你喜欢

转载自blog.csdn.net/weixin_38246633/article/details/80781005