Skip to content

Commit a2c35c6

Browse files
committed
LeetCode #206: Reverse Linked List
1 parent e153484 commit a2c35c6

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

prep/practice/linked_list.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import Optional
2+
3+
4+
class ListNode:
5+
def __init__(self, val=0, next=None):
6+
self.val = val
7+
self.next = next
8+
9+
10+
def print_list(head):
11+
current = head
12+
while current is not None:
13+
print(current.val, end=" ")
14+
current = current.next
15+
print()
16+
17+
18+
class Solution:
19+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
20+
prev = None
21+
current = head
22+
23+
while current:
24+
next_node = current.next
25+
current.next = prev
26+
prev = current
27+
current = next_node
28+
return prev

0 commit comments

Comments
 (0)