forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (59 loc) · 1.61 KB
/
main.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
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
# 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.
'''
Created on 2013-5-20
@author: Yubin Bai
'''
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# @param a ListNode
# @return a ListNode
def swapPairs(self, head):
if head == None:
return None
if head.next == None:
return head
prev = head
curr = head.next
newHead = curr
# swap in pairs
while True:
nextPrev = curr.next
if nextPrev == None: # reached end of list
curr.next = prev
prev.next = None
break
nextCurr = nextPrev.next
if nextCurr == None:
curr.next = prev
prev.next = nextPrev
break
curr.next = prev
prev.next = nextCurr
prev = nextPrev
curr = nextCurr
return newHead
def printList(head):
curr = head
result = []
while curr != None:
result.append(curr.val)
curr = curr.next
print(result)
if __name__ == '__main__':
s = Solution()
linked = ListNode(0)
curr = linked
for i in range(1, 5):
curr.next = ListNode(i)
curr = curr.next
printList(linked)
linked2 = s.swapPairs(linked)
printList(linked2)