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.
2 parents ae7e917 + a59a788 commit 7a67d6eCopy full SHA for 7a67d6e
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,21 @@ def reverse_list(head):
17
21
current.next = prev
18
22
prev = current
19
23
return prev
24
+
25
26
27
+# Recursive solution
28
29
30
+def reverse_list_recursive(head):
31
+ """
32
+ :type head: ListNode
33
+ :rtype: ListNode
34
35
+ if head is None or head.next is None:
36
+ return head
37
+ p = head.next
38
+ head.next = None
39
+ revrest = self.reverse(p)
40
+ p.next = head
41
+ return revrest
0 commit comments