-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24.py
More file actions
46 lines (41 loc) · 1.04 KB
/
24.py
File metadata and controls
46 lines (41 loc) · 1.04 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
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
dummy_head= ListNode(0)
dummy_head.next=head
pre=dummy_head
cur=head
while cur is not None and cur.next is not None:
next_node=cur.next
tail_node=next_node.next
pre.next=next_node
next_node.next=cur
cur.next=tail_node
pre=cur
cur=pre.next
return dummy_head.next
def init_linked_list(lst):
if lst is None:
return None
head=ListNode(lst[0])
cur=head
for i in lst[1:]:
cur.next=ListNode(i)
cur=cur.next
return head
input_list = [1]
head = init_linked_list(input_list)
s=Solution()
head= s.swapPairs(head)
current = head
while current:
print(current.val)
current = current.next