-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path24. Swap_Nodes_in_Pairs.py
More file actions
68 lines (55 loc) · 1.76 KB
/
24. Swap_Nodes_in_Pairs.py
File metadata and controls
68 lines (55 loc) · 1.76 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def create_link(nums):
head = ListNode(None)
cur_node = head
for i in range(len(nums)):
cur_node.next = ListNode(nums[i])
cur_node = cur_node.next
return head.next
def check_link(link):
while link != None:
print(link.val, end=' ')
link = link.next
print()
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None: return None
if head.next == None: return head
if head.next.next == None:
new_head = head.next
new_head.next = head
new_head.next.next = None
return new_head
new_head = ListNode(0)
new_head.next = head
font_point = rear_point = new_head
for i in range(3): font_point = font_point.next
while font_point != None:
tmp = rear_point.next
rear_point.next = rear_point.next.next
rear_point.next.next = tmp
tmp.next = font_point
rear_point = rear_point.next.next
if font_point.next != None:
if font_point.next.next != None:
font_point = font_point.next.next
else:
rear_point.next = rear_point.next.next
rear_point.next.next = font_point
font_point.next = None
return new_head.next
else:
return new_head.next
if __name__ == '__main__':
l = create_link([1, 2, 3, 4])
s = Solution()
result = s.swapPairs(l)
check_link(result)