美文网首页
Leetcode-206题:Reverse Linked Lis

Leetcode-206题:Reverse Linked Lis

作者: 八刀一闪 | 来源:发表于2016-09-26 22:21 被阅读8次

题目

Reverse a singly linked list.

代码

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head == None:
            return None
        tail = head
        while tail.next != None:
            next = tail.next
            tail.next = next.next
            next.next = head
            head = next
        return head

相关文章

网友评论

      本文标题:Leetcode-206题:Reverse Linked Lis

      本文链接:https://www.haomeiwen.com/subject/qgaqyttx.html