Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ec908ad

Browse files
authored
Update Swap_Nodes_in_Pairs.py
1 parent 8dfbf1f commit ec908ad

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

‎Linked_Lists/Swap_Nodes_in_Pairs.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Solution - 1: Swapping the Nodes | Time: O(N) and Space: O(1)
1+
# Solution - 1: Swapping the Nodes (Iterative) | Time: O(N) and Space: O(1)
22
class Solution:
33
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
44
# Base condition
@@ -22,8 +22,19 @@ def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
2222

2323
return t_head
2424

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
2535

26-
# 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)
2738
class Solution:
2839
def swapPairs(self, head: ListNode) -> ListNode:
2940
if not head or not head.next:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /