-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathreverse-linked-list.py
More file actions
43 lines (31 loc) · 1.02 KB
/
reverse-linked-list.py
File metadata and controls
43 lines (31 loc) · 1.02 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
'''
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
'''
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseList(self, head, prev = None):
"""
:type head: ListNode
:rtype: ListNode
"""
# Approach one 迭代思路:重复一定的算法,达到想要的目的。数学上二分法,牛顿法是很好的迭代例子
# while head:
# curr = head
# head = head.next
# curr.next = prev
# prev = curr
# return prev
# Approach two 递归思路:在return处调用自己(尾递归)
if not head:
return prev
curr, head.next = head.next, prev # 新旧链表的两个方向同时前进
return self.reverseList(curr, head)