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 508129a commit 78dae2dCopy full SHA for 78dae2d
βreverse-linked-list/jungsiroo.pyβ
@@ -0,0 +1,29 @@
1
+class Solution:
2
+ def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
3
+ # iterative way
4
+
5
+ """
6
+ prev, curr = None, head
7
+ while curr:
8
+ tmp_nxt = curr.next
9
10
+ curr.next = prev
11
+ prev, curr = curr, tmp_nxt
12
13
+ return prev
14
15
16
+ # Recursive Way
17
+ if head is None or head.next is None:
18
+ return head
19
20
+ new_head = self.reverseList(head.next)
21
+ head.next.next = head # reversing pointer
22
+ head.next = None
23
+ return new_head
24
25
+ # λ λ€ μκ°λ³΅μ‘λ O(n)
26
+ # νμ§λ§ μ¬κ·μ κ²½μ° μ½μ€νμ λ°λ₯Έ 곡κ°λ³΅μ‘λ O(n)μ μμ
27
+ # iterative λ°©μμ O(1)
28
29
0 commit comments