一、题目
Reverse Linked List二、解题
单向列表的倒序。
三、尝试与结果
三个指针:
class Solution(object):
def reverseList(self, head):
if head == None:
return None
if head.next == None:
return head
front = head
last = None
mid = None
while front.next != None:
mid = front
front = front.next
mid.next = last
last = mid
front.next = mid
return front
结果:AC
递归:
class Solution(object):
def reverseList(self, head):
if head == None:
return None
if head.next == None:
return head
last = head.next
result = self.reverseList(last)
head.next = None
last.next = head
return result
结果:AC
网友评论