-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24. Swap Nodes in Pairs.py
More file actions
38 lines (35 loc) · 928 Bytes
/
24. Swap Nodes in Pairs.py
File metadata and controls
38 lines (35 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
# start=head
# end=head
# head=head.next
#
# while True:
# if end==None: break
# end=end.next
# if end==None: break
# end=end.next
# start.next.next=start
# start.next=end
# print(end.val)
#
# return head
result=ListNode(0)
top1=head
top2=result
while True:
start=top1
if start==None: break
end=top1.next
if end==None: break
top1=end.next.next
top2.next=end
top2=top2.next
top2.next=start
top2=top2.next
return result.next