链表_leetcode206

# 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 not head or not head.next:
return head

pre = None
cur = head

while cur:
next = cur.next
cur.next = pre
pre = cur
cur = next


return pre


# 递归解决
class Solution2(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""

if not head or not head.next:
return head

rhead = self.reverseList(head.next)

head.next.next = head
head.next = None

return rhead

猜你喜欢

转载自www.cnblogs.com/lux-ace/p/10557232.html