Skip to content

Commit bb5e360

Browse files
committed
feat: Add linked_list.py
1 parent 3e19cc3 commit bb5e360

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/linked_list.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Linked List operations module.
3+
4+
This module provides functionality for manipulating singly linked lists,
5+
including operations like reversing the list.
6+
"""
7+
8+
9+
class ListNode:
10+
def __init__(self, val=None, next=None):
11+
self.val = val
12+
self.next = next
13+
14+
15+
def reverse_linked_list(head: ListNode) -> ListNode:
16+
"""
17+
Reverses a singly linked list.
18+
19+
Args:
20+
head (ListNode): The head node of the linked list
21+
22+
Returns:
23+
ListNode: The head node of the reversed linked list
24+
"""
25+
prev = None
26+
current = head
27+
28+
while current is not None:
29+
next_temp = current.next
30+
current.next = prev
31+
prev = current
32+
current = next_temp
33+
34+
return prev

0 commit comments

Comments
 (0)