-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReverseList.py
More file actions
46 lines (40 loc) · 1.17 KB
/
ReverseList.py
File metadata and controls
46 lines (40 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 206. Reverse List
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# 运用递归方法(64ms):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head or not head.next:
return head
# reverseList(head)返回输入的链表反转后的head,那么如果reverseList(head.next)的话
# 1(head)->2<-3<-4<-5(ans)
ans = self.reverseList(head.next)
# 我们此时只需要head.next.next=head,也就是先建立一个双向连接
# 1(head)<-2<-3<-4<-5(ans)
head.next.next = head
# 然后再head.next=None,返回ans即可
# null<-1(head)<-2<-3<-4<-5(ans)
head.next = None
return ans
class Solution2:
# 运用迭代方法(48ms)
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return head
reverse = None
while head:
next = reverse
reverse = head
head = head.next
reverse.next = next
return reverse