-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24.SwapNodesInPairs.py
More file actions
45 lines (42 loc) · 1.31 KB
/
24.SwapNodesInPairs.py
File metadata and controls
45 lines (42 loc) · 1.31 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
# 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
:note:
定义三个节点:pre、cur、curNext,先交换cur和curNext,然后更新和移动pre节点。
"""
def printList(head):
while(head):
print head.val,
head = head.next
print ''
if head == None or head.next == None:
return head
pre = None
cur = head
curNext = cur.next
while(True):
print pre if pre==None else pre.val, cur.val, curNext.val
printList(head)
if pre == None:
head = curNext
cur.next = curNext.next
curNext.next = cur
pre = cur
else:
cur.next = curNext.next
curNext.next = cur
pre.next = curNext
pre = cur
printList(head)
if cur.next == None or cur.next.next == None:
break
cur = cur.next
curNext = cur.next
return head