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 8dfbf1f commit ec908adCopy full SHA for ec908ad
Linked_Lists/Swap_Nodes_in_Pairs.py
@@ -1,4 +1,4 @@
1
-# Solution - 1: Swapping the Nodes | Time: O(N) and Space: O(1)
+# Solution - 1: Swapping the Nodes (Iterative) | Time: O(N) and Space: O(1)
2
class Solution:
3
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
4
# Base condition
@@ -22,8 +22,19 @@ def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
22
23
return t_head
24
25
+# Solution - 1 Swapping the Nodes (Recursive) | Time: O(N) and Space: O(N)
26
+class Solution:
27
+ def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
28
+ if head and head.next:
29
+ temp = head.next
30
+ head.next = self.swapPairs(temp.next)
31
+ temp.next = head
32
+ return temp
33
+
34
+ return head
35
-# Solution - 2: Swapping the Values | Time: O(N) and Space: O(1)
36
37
+ # Solution - 2: Swapping the Values | Time: O(N) and Space: O(1)
38
39
def swapPairs(self, head: ListNode) -> ListNode:
40
if not head or not head.next:
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments