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