-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list_reversal.py
More file actions
69 lines (58 loc) · 1.63 KB
/
linked_list_reversal.py
File metadata and controls
69 lines (58 loc) · 1.63 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
59
60
61
62
63
64
65
66
67
68
'''
Given the head of a singly linked list, reverse the list, and return the reversed list.
Example:
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
'''
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# --- Helper Functions for Testing ---
def create_linked_list(arr):
"""Converts a Python list to a Linked List."""
if not arr:
return None
head = ListNode(arr[0])
curr = head
for val in arr[1:]:
curr.next = ListNode(val)
curr = curr.next
return head
def linked_list_to_list(head):
"""Converts a Linked List back to a Python list for easy assertion."""
result = []
curr = head
while curr:
result.append(curr.val)
curr = curr.next
return result
# --- The Function to Test ---
def reverseList(head):
prev = None
curr = head
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
return prev
# --- The Test Function ---
def test_reverse():
# Case 1: Standard list
input_arr = [1, 2, 3, 4, 5]
head = create_linked_list(input_arr)
reversed_head = reverseList(head)
assert linked_list_to_list(reversed_head) == [5, 4, 3, 2, 1]
# Case 2: Single element
head = create_linked_list([1])
reversed_head = reverseList(head)
assert linked_list_to_list(reversed_head) == [1]
# Case 3: Empty list
head = create_linked_list([])
reversed_head = reverseList(head)
assert linked_list_to_list(reversed_head) == []
print("All tests passed! ✅")
# Run the test
if __name__ == "__main__":
test_reverse()