Skip to content

Commit 24f20d7

Browse files
committed
LeetCode 206. Reverse Linked List
1 parent ae52fac commit 24f20d7

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Proposed solutions to some LeetCode problems. The first column links to the prob
3030
| [160. Intersection of Two Linked Lists][lc160] | Easy | [python](leetcode/intersection-of-two-linked-lists.py) |
3131
| [167. Two Sum II - Input Array Is Sorted][lc167] | Easy | [python](leetcode/two-sum-ii-input-array-is-sorted.py) |
3232
| [205. Isomorphic Strings][lc205] | Easy | [python](leetcode/isomorphic-strings.py) |
33+
| [206. Reverse Linked List][lc206] | Easy | [python](leetcode/reverse-linked-list.py) |
3334
| [208. Implement Trie (Prefix Tree)][lc208] | Medium | [python](leetcode/implement-trie-prefix-tree.py) |
3435
| [211. Design Add and Search Words Data Structure][lc211] | Medium | [python](leetcode/design-add-and-search-words-data-structure.py) |
3536
| [215. Kth Largest Element in an Array][lc215] | Medium | [python](leetcode/kth-largest-element-in-an-array.py) |
@@ -90,6 +91,7 @@ Proposed solutions to some LeetCode problems. The first column links to the prob
9091
[lc160]: https://leetcode.com/problems/intersection-of-two-linked-lists/
9192
[lc167]: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
9293
[lc205]: https://leetcode.com/problems/isomorphic-strings/
94+
[lc206]: https://leetcode.com/problems/reverse-linked-list/
9395
[lc208]: https://leetcode.com/problems/implement-trie-prefix-tree/
9496
[lc211]: https://leetcode.com/problems/design-add-and-search-words-data-structure/
9597
[lc215]: https://leetcode.com/problems/kth-largest-element-in-an-array/

leetcode/data.py

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ def __init__(self):
2020
self.children = defaultdict(TrieNode)
2121
self.isWord = False
2222

23+
24+
# Definition for singly-linked list.
25+
class ListNode:
26+
def __init__(self, val=0, next=None):
27+
self.val = val
28+
self.next = next
29+
2330
# Useful for testing binary tree problems using the strings provided on the description
2431
# https://leetcode.com/problems/recover-binary-search-tree/discuss/32539/Tree-Deserializer-and-Visualizer-for-Python
2532

leetcode/merge_two_sorted_lists.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from typing import List, Optional
55

6-
from leetcode.helpers import BColors
6+
from helpers import BColors
77

88

99
# Definition for singly-linked list.
@@ -13,6 +13,8 @@ def __init__(self, val=0, next=None):
1313
self.next = next
1414

1515

16+
# Runtime: 40 ms, faster than 89.68% of Python3 online submissions for Merge Two Sorted Lists.
17+
# Memory Usage: 14 MB, less than 32.07 % of Python3 online submissions for Merge Two Sorted Lists.
1618
class Solution:
1719
def mergeTwoLists(self, node1: Optional[ListNode], node2: Optional[ListNode]) -> Optional[ListNode]:
1820
head_pointer = temp = ListNode()

leetcode/reverse-linked-list.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# https://leetcode.com/problems/reverse-linked-list/
2+
3+
4+
from typing import List, Optional
5+
from data import ListNode
6+
7+
8+
# Runtime: 42 ms, faster than 81.83% of Python3 online submissions for Reverse Linked List.
9+
# Memory Usage: 15.4 MB, less than 55.68 % of Python3 online submissions for Reverse Linked List.
10+
class Solution:
11+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
12+
if not head:
13+
return
14+
current = head
15+
next = current.next
16+
while next:
17+
prev = current
18+
current = next
19+
next = current.next
20+
current.next = prev
21+
head.next = None
22+
return current
23+
24+
25+
def linkedListToArray(result: List[int], node: ListNode) -> List[int]:
26+
result.append(node.val)
27+
if node.next:
28+
return linkedListToArray(result, node.next)
29+
else:
30+
return result
31+
32+
33+
def test():
34+
node5 = ListNode(5)
35+
node4 = ListNode(4, node5)
36+
node3 = ListNode(3, node4)
37+
node2 = ListNode(2, node3)
38+
node1 = ListNode(1, node2)
39+
sol = Solution()
40+
result = linkedListToArray([], sol.reverseList(node1))
41+
expected = [5, 4, 3, 2, 1]
42+
assert result == expected, f'{result} != {expected}'
43+
assert sol.reverseList(None) == None
44+
45+
46+
test()

0 commit comments

Comments
 (0)