We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent e153484 commit a2c35c6Copy full SHA for a2c35c6
1 file changed
prep/practice/linked_list.py
@@ -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
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