Skip to content

Commit 2c219d9

Browse files
Leetcode 24
1 parent 44e1fed commit 2c219d9

1 file changed

Lines changed: 246 additions & 0 deletions

File tree

Leetcode/Leetcode_24.py

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
# ============================================================
2+
# 24. Swap Nodes in Pairs
3+
# LeetCode: https://leetcode.com/problems/swap-nodes-in-pairs/
4+
#
5+
# Approach:
6+
# ----------
7+
# We use a dummy node to simplify swapping the first pair.
8+
#
9+
# For every iteration:
10+
#
11+
# Before Swap:
12+
#
13+
# prev
14+
# |
15+
# v
16+
# dummy -> 1 -> 2 -> 3 -> 4
17+
# ^ ^
18+
# first second
19+
#
20+
# Step 1:
21+
# first.next = second.next
22+
#
23+
# dummy -> 1 -----> 3 -> 4
24+
# \
25+
# X
26+
# 2
27+
#
28+
# Step 2:
29+
# second.next = first
30+
#
31+
# dummy 2 -> 1 -> 3 -> 4
32+
#
33+
# Step 3:
34+
# prev.next = second
35+
#
36+
# dummy -> 2 -> 1 -> 3 -> 4
37+
#
38+
# Move prev to the end of the swapped pair:
39+
#
40+
# prev = first
41+
#
42+
# Repeat until fewer than two nodes remain.
43+
#
44+
# ------------------------------------------------------------
45+
# Time Complexity : O(n)
46+
# - Each node is visited exactly once.
47+
#
48+
# Space Complexity : O(1)
49+
# - Only a few pointers are used.
50+
# ============================================================
51+
52+
# Definition for singly-linked list.
53+
# class ListNode:
54+
# def __init__(self, val=0, next=None):
55+
# self.val = val
56+
# self.next = next
57+
58+
59+
class Solution:
60+
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
61+
# --------------------------------------------------------
62+
# Step 1: Create a dummy node before the head.
63+
# This helps handle swapping the first pair easily.
64+
# --------------------------------------------------------
65+
dummy = ListNode(0)
66+
dummy.next = head
67+
68+
# Pointer used to traverse the list.
69+
prev = dummy
70+
71+
# --------------------------------------------------------
72+
# Continue while there are at least two nodes to swap.
73+
# --------------------------------------------------------
74+
while prev.next and prev.next.next:
75+
76+
# First and second nodes of the current pair
77+
first = prev.next
78+
second = first.next
79+
80+
# ----------------------------------------------------
81+
# Swap the two nodes
82+
# ----------------------------------------------------
83+
84+
# Connect first node to the node after second
85+
first.next = second.next
86+
87+
# Put second before first
88+
second.next = first
89+
90+
# Connect previous part to second
91+
prev.next = second
92+
93+
# Move prev to the end of the swapped pair
94+
prev = first
95+
96+
# New head of the modified list
97+
return dummy.next
98+
99+
100+
"""
101+
==========================
102+
Example Dry Run
103+
==========================
104+
105+
Input:
106+
1 -> 2 -> 3 -> 4
107+
108+
Initial:
109+
110+
dummy -> 1 -> 2 -> 3 -> 4
111+
^
112+
prev
113+
114+
115+
--------------------------------
116+
Iteration 1
117+
--------------------------------
118+
119+
first = 1
120+
second = 2
121+
122+
Step 1:
123+
first.next = second.next
124+
125+
1 -> 3
126+
127+
Step 2:
128+
second.next = first
129+
130+
2 -> 1 -> 3
131+
132+
Step 3:
133+
prev.next = second
134+
135+
dummy -> 2 -> 1 -> 3 -> 4
136+
137+
Move:
138+
prev = first
139+
140+
dummy -> 2 -> 1 -> 3 -> 4
141+
^
142+
prev
143+
144+
145+
--------------------------------
146+
Iteration 2
147+
--------------------------------
148+
149+
first = 3
150+
second = 4
151+
152+
Step 1:
153+
3.next = None
154+
155+
Step 2:
156+
4.next = 3
157+
158+
Step 3:
159+
1.next = 4
160+
161+
Result:
162+
163+
dummy -> 2 -> 1 -> 4 -> 3
164+
165+
Move:
166+
prev = 3
167+
168+
Loop Ends.
169+
170+
Return:
171+
dummy.next
172+
173+
Output:
174+
2 -> 1 -> 4 -> 3
175+
176+
177+
==========================
178+
Edge Cases
179+
==========================
180+
181+
Case 1:
182+
Input:
183+
[]
184+
185+
Output:
186+
[]
187+
188+
--------------------------------
189+
190+
Case 2:
191+
Input:
192+
[1]
193+
194+
Output:
195+
[1]
196+
197+
--------------------------------
198+
199+
Case 3:
200+
Input:
201+
[1,2]
202+
203+
Output:
204+
[2,1]
205+
206+
--------------------------------
207+
208+
Case 4:
209+
Input:
210+
[1,2,3]
211+
212+
Output:
213+
[2,1,3]
214+
215+
--------------------------------
216+
217+
Case 5:
218+
Input:
219+
[1,2,3,4,5]
220+
221+
Output:
222+
[2,1,4,3,5]
223+
224+
225+
==========================
226+
Complexity Analysis
227+
==========================
228+
229+
Time Complexity:
230+
O(n)
231+
232+
Reason:
233+
- Every node is processed exactly once.
234+
- Each swap takes constant time.
235+
236+
Space Complexity:
237+
O(1)
238+
239+
Reason:
240+
- No extra list or recursion is used.
241+
- Only four pointers are maintained:
242+
• dummy
243+
• prev
244+
• first
245+
• second
246+
"""

0 commit comments

Comments
 (0)