-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc24.py
More file actions
52 lines (41 loc) · 904 Bytes
/
Copy pathlc24.py
File metadata and controls
52 lines (41 loc) · 904 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
__author__ = 'linx'
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
pass
class Solution(object):
def swapPairs(self, head):
if head == None:
return
if head.next == None:
return ListNode(head.val)
first, second = head.next, head
retHead = ListNode(first.val)
curr = retHead
while first != None or second != None:
curr.next = ListNode(second.val)
curr = curr.next
second = first.next
if second == None:
break
first = second.next
if first == None:
curr.next = ListNode(second.val)
break
curr.next = ListNode(first.val)
curr = curr.next
return retHead
pass
def main():
orgHead = ListNode(1)
curr = orgHead
for i in xrange(2):
curr.next = ListNode(i + 2)
curr = curr.next
tmp = Solution().swapPairs(orgHead)
while tmp != None:
print tmp.val,
tmp = tmp.next
if __name__ == '__main__':
main()