Skip to content

Commit 2cefe92

Browse files
committed
add solution: reverse-linked-list
1 parent 50a6e9e commit 2cefe92

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

reverse-linked-list/dusunax.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
# 206. Reverse Linked List
3+
4+
iterate through the linked list and reverse the direction of the pointers.
5+
6+
## Time and Space Complexity
7+
8+
```
9+
TC: O(n)
10+
SC: O(1)
11+
```
12+
'''
13+
class Solution:
14+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
15+
prev = None
16+
current = head
17+
18+
while current is not None: # TC: O(n)
19+
next_list_temp = current.next
20+
current.next = prev
21+
prev = current
22+
current = next_list_temp
23+
24+
return prev

0 commit comments

Comments
 (0)