-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0206__reverse_linked_list.py
57 lines (37 loc) · 1.32 KB
/
0206__reverse_linked_list.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
"""
Given the head of a singly linked list, reverse the list, and return the reversed list.
## Example 1
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
## Example 2
Input: head = [1,2]
Output: [2,1]
## Example 3
Input: head = []
Output: []
## Constraints
* The number of nodes in the list is the range [0, 5000].
* -5000 <= Node.val <= 5000
## Follow up
A linked list can be reversed either iteratively or recursively. Could you implement both?
"""
from typing import Optional
from unittest import TestCase
from lib.ListNode import ListNode, build_list, flatten_list
class Solution(TestCase):
def test_example_1(self):
head = build_list([1, 2, 3, 4, 5])
self.assertEqual([5, 4, 3, 2, 1], flatten_list(self.reverseList(head)))
def test_example_2(self):
self.assertEqual([2, 1], flatten_list(self.reverseList(build_list([1, 2]))))
def test_example_3(self):
self.assertEqual([], flatten_list(self.reverseList(None)))
def test_single_node(self):
self.assertEqual([1], flatten_list(self.reverseList(build_list([1]))))
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head
parent = self.reverseList(head.next)
head.next.next = head
head.next = None
return parent