题解 | #反转链表#

反转链表

http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

step1: 判断输入的链表是否为空,若是就直接返回
step2:定义两个指针pre,post,并赋值为None
step3:撰写一个while循环,进行遍历
class ListNode(Object):
    def __init__(self, x=0, next=None):
        self.x = x
        self.next = next

class Solution(Object):
    def reverseList(self, head):
        if not head:
            return head
        else:
            pre,post = None, None
            while(head):
                post = head.next
                head.next = pre
                pre = head
                head = post
            head = pre
            return head
        
    


全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务