forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_linked_list.py
More file actions
58 lines (45 loc) · 1.23 KB
/
reverse_linked_list.py
File metadata and controls
58 lines (45 loc) · 1.23 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
47
48
49
50
51
52
53
54
55
56
57
58
"""
https://www.enjoyalgorithms.com/blog/reverse-linked-list
"""
class ListNode:
"""Definition for singly-linked list."""
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverse_linked_list(head: ListNode) -> ListNode:
"""
Reverse a singly linked list.
Args:
head: The head node of the linked list.
Returns:
The new head node of the reversed linked list.
Examples:
>>> a = ListNode(1)
>>> b = ListNode(2)
>>> c = ListNode(3)
>>> a.next, b.next = b, c
>>> head = reverse_linked_list(a)
>>> [head.val, head.next.val, head.next.next.val]
[3, 2, 1]
"""
prev = None
current = head
while current:
nxt = current.next
current.next = prev
prev = current
current = nxt
return prev
if __name__ == "__main__":
import doctest
doctest.testmod()
# Example execution
a = ListNode(1)
b = ListNode(2)
c = ListNode(3)
a.next, b.next = b, c
print("Original Linked List: 1 -> 2 -> 3")
new_head = reverse_linked_list(a)
print(
f"Reversed Linked List: {new_head.val} -> {new_head.next.val} -> {new_head.next.next.val}"
)