Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 28b2a11

Browse files
authored
Create Reverse_Linked_List.py
1 parent c591898 commit 28b2a11

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Linked_Lists/Reverse_Linked_List.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Iterative | Time: O(N) & Space: O(1)
2+
3+
class Solution:
4+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
5+
prev = None
6+
while head:
7+
x = head.next
8+
head.next = prev
9+
prev = head
10+
head = x
11+
12+
return prev

0 commit comments

Comments
 (0)