We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b885adc commit cede840Copy full SHA for cede840
reverse-linked-list/prograsshopper.py
@@ -0,0 +1,20 @@
1
+# Definition for singly-linked list.
2
+# class ListNode:
3
+# def __init__(self, val=0, next=None):
4
+# self.val = val
5
+# self.next = next
6
+class Solution:
7
+ def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
8
+ # time complexity: O(n) / memory complexity: O(n)
9
+ stack = []
10
+ current = head
11
+ while current:
12
+ stack.append(current.val)
13
+ current = current.next
14
+
15
+ dummy_head = ListNode()
16
+ current = dummy_head
17
+ while stack:
18
+ current.next = ListNode(val=stack.pop(), next=None)
19
20
+ return dummy_head.next
0 commit comments