-
Notifications
You must be signed in to change notification settings - Fork 0
/
24_swap_nodes_in_pairs.py
35 lines (32 loc) · 1.03 KB
/
24_swap_nodes_in_pairs.py
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
'''
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
'''
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
cur = head
if cur != None and cur.next != None:
head = cur.next
while cur != None and cur.next != None:
next_cur = cur.next.next
cur.next.next = cur
if next_cur == None:
cur.next = None
elif next_cur.next == None:
cur.next = next_cur
else:
cur.next = next_cur.next
#print 'cur', cur.val, cur.next.val
cur = next_cur
return head