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 ae7e917 commit 95486d6Copy full SHA for 95486d6
linkedlist/reverse.py
@@ -3,6 +3,10 @@
3
"""
4
5
6
+#
7
+# Iterative solution
8
+# T(n)- O(n)
9
10
def reverse_list(head):
11
12
:type head: ListNode
@@ -17,3 +21,16 @@ def reverse_list(head):
17
21
current.next = prev
18
22
prev = current
19
23
return prev
24
+
25
26
+# Recursive solution
27
28
29
+def reverse_list_recursive(head):
30
+ if head is None or head.next is None:
31
+ return head
32
+ p = head.next
33
+ head.next = None
34
+ revrest = self.reverse(p)
35
+ p.next = head
36
+ return revrest
0 commit comments