Skip to content

Commit b701aa3

Browse files
committed
Reverse Linked List
1 parent 416c12e commit b701aa3

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

reverse_linked_list.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Write a function that takes in the head of a Singly Linked List
2+
# (assume that the list has at least 1 node; in other words, the head will never be a null value).
3+
# The function should reverse the list and return its new head. Note that every node in the Singly Linked List
4+
# has a "value" property storing its value as well as a "next" property pointing to the next node in the list
5+
# or None (null) if it is the tail of the list.
6+
7+
8+
def reverseLinkedList(head):
9+
p1, p2 = None, head
10+
while p2:
11+
p3 = p2.next
12+
p2.next = p1
13+
p1 = p2
14+
p2 = p3
15+
return p1

0 commit comments

Comments
 (0)