链表_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
posted @ 2019-03-19 11:00  AceKo  阅读(99)  评论(0编辑  收藏  举报