-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path206.反转链表.py
73 lines (64 loc) · 1.54 KB
/
206.反转链表.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#
# @lc app=leetcode.cn id=206 lang=python3
#
# [206] 反转链表
#
# https://leetcode-cn.com/problems/reverse-linked-list/description/
#
# algorithms
# Easy (66.97%)
# Likes: 766
# Dislikes: 0
# Total Accepted: 156.8K
# Total Submissions: 234.1K
# Testcase Example: '[1,2,3,4,5]'
#
# 反转一个单链表。
#
# 示例:
#
# 输入: 1->2->3->4->5->NULL
# 输出: 5->4->3->2->1->NULL
#
# 进阶:
# 你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
#
#
# @lc code=start
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
# # 1. 迭代-双指针 O(N) O(1)
# pre, cur = None, head
# # 1.1
# while cur:
# cur.next, pre, cur = pre, cur, cur.next
# return pre
# # 1.2
# while cur:
# tmp = cur.next
# cur.next = pre
# pre = cur
# cur = tmp
# return pre
# 2. 递归 O(N) O(N)
# 2.1
# if not head or not head.next:
# return head
# res = self.reverseList(head.next)
# head.next.next = head
# head.next = None
# return res
# 2.2
def recur(pre, cur):
if not cur:
return pre
res = recur(cur, cur.next)
cur.next = pre
return res
return recur(None, head)
# @lc code=end