Skip to content

Commit 905a28c

Browse files
authored
Merge pull request vJechsmayr#212 from bislara/0024_swap_nodes
added swap notes in pair solution
2 parents 424e446 + 4d24827 commit 905a28c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
Problem:-
3+
4+
Given a linked list, swap every two adjacent nodes and return its head.
5+
You may not modify the values in the list's nodes, only nodes itself may be changed.
6+
7+
Example:
8+
- Given 1->2->3->4, you should return the list as 2->1->4->3.
9+
10+
'''
11+
12+
# Definition for singly-linked list.
13+
# class ListNode:
14+
# def __init__(self, val=0, next=None):
15+
# self.val = val
16+
# self.next = next
17+
class Solution:
18+
def swapPairs(self, head: ListNode) -> ListNode:
19+
pre, pre.next = self, head
20+
while pre.next and pre.next.next:
21+
a = pre.next
22+
b = a.next
23+
pre.next, b.next, a.next = b, a, b.next
24+
pre = a
25+
return self.next
26+
27+

0 commit comments

Comments
 (0)