Skip to content

Commit 2e25924

Browse files
author
yangdi
committed
update leetcode
1 parent 117e2ff commit 2e25924

File tree

4 files changed

+185
-10
lines changed

4 files changed

+185
-10
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
'''
2+
You are given two linked lists representing two non-negative numbers.
3+
The digits are stored in reverse order and each of their nodes contain a single digit.
4+
Add the two numbers and return it as a linked list.
5+
6+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
7+
Output: 7 -> 0 -> 8
8+
342 + 465 = 807
9+
'''
10+
11+
class ListNode:
12+
def __init__(self, x):
13+
self.val = x
14+
self.next = None
15+
16+
# Definition for singly-linked list.
17+
# class ListNode:
18+
# def __init__(self, x):
19+
# self.val = x
20+
# self.next = None
21+
# optimize cause too much code.
22+
23+
class Solution:
24+
25+
# @return carry, result
26+
def splitSum(self, s):
27+
if s >= 10:
28+
return 1, s-10
29+
else:
30+
return 0, s
31+
32+
# @return new node
33+
def addValue(self, node, sum):
34+
node.next = ListNode(sum)
35+
return node.next
36+
37+
# @return a ListNode
38+
def addTwoNumbers(self, l1, l2):
39+
if l1 == None and l2 == None:
40+
return None
41+
42+
headValue = 0
43+
if l1 != None:
44+
headValue += l1.val
45+
l1 = l1.next
46+
47+
if l2 != None:
48+
headValue += l2.val
49+
l2 = l2.next
50+
51+
carry, headValue = self.splitSum(headValue)
52+
result = head = ListNode(headValue)
53+
54+
while l1 != None and l2 != None:
55+
carry, sum = self.splitSum(l1.val + l2.val + carry)
56+
result = self.addValue(result, sum)
57+
58+
l1 = l1.next
59+
l2 = l2.next
60+
61+
while l1 != None:
62+
carry, sum = self.splitSum(l1.val + carry)
63+
result = self.addValue(result, sum)
64+
l1 = l1.next
65+
66+
while l2 != None:
67+
carry, sum = self.splitSum(l2.val + carry)
68+
result = self.addValue(result, sum)
69+
l2 = l2.next
70+
71+
if carry == 1:
72+
result = self.addValue(result, 1)
73+
return head
74+
75+
def makeList(self, src):
76+
if src == None:
77+
return None
78+
79+
p = head = ListNode(src[0])
80+
for i in range(1, len(src)):
81+
p = self.addValue(p, src[i])
82+
return head
83+
84+
makeList = Solution().makeList
85+
86+
def show(l):
87+
p = l
88+
while p != None:
89+
print p.val
90+
p = p.next
91+
92+
def list2Array(l):
93+
r = list()
94+
while l != None:
95+
r.append(l.val)
96+
l = l.next
97+
return r
98+
99+
def test(l1, l2, expect):
100+
result = Solution().addTwoNumbers(makeList(l1), makeList(l2))
101+
result = list2Array(result)
102+
if expect != result:
103+
print "l1:", l1
104+
print "l2:", l2
105+
print "expect:", expect
106+
print "result:", result
107+
108+
if __name__ == '__main__':
109+
test([2, 4, 3], [5, 6, 4], [7, 0, 8])
110+
test([2, 4, 3], [5, 6, 4, 1], [7, 0, 8, 1])
111+
test([2, 4, 3, 1], [5, 6, 4], [7, 0, 8, 1])
112+

src2/SortList/sort_list.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Sort a linked list in O(n log n) time using constant space complexity.
33
'''
44

5+
import sys
6+
sys.path.append('../../test_framework')
7+
from test_framework import *
8+
59
# Definition for singly-linked list.
610
# class ListNode:
711
# def __init__(self, x):
@@ -11,16 +15,32 @@
1115
class Solution:
1216
# @param head, a ListNode
1317
# @return a ListNode
14-
def sortList(self, head):
15-
p = head
16-
last = head
17-
while p.next != None::
18-
q = p.next
19-
if q.val > p.val:
20-
q.val, p.val = p.val, q.val
21-
22-
return head
18+
def quickSortList(self, head):
19+
middle = head
20+
l = r = None
21+
22+
p = head.next
23+
lp = rp = middle
24+
25+
while p != None:
26+
pNext = p.next
27+
if p.val >= middle.val:
28+
p.next = lp
29+
lp = p
30+
else:
31+
rp.next = p
32+
rp = p
33+
p = pNext
2334

35+
rp.next = None
36+
return lp
37+
38+
def sortList(self, head):
39+
# p = head
40+
head = self.quickSortList(head)
41+
head.show()
2442

2543
if __name__ == '__main__':
26-
Solution().sortList()
44+
head = makeList([5, 4, 5, 3, 7])
45+
# head.show()
46+
Solution().sortList(head)

test_framework/__init__.py

Whitespace-only changes.

test_framework/test_framework.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
3+
class ListNode:
4+
def __init__(self, x):
5+
self.val = x
6+
self.next = None
7+
8+
# @return next node
9+
def addNext(self, sum):
10+
self.next = ListNode(sum)
11+
return self.next
12+
13+
# @param [in] list
14+
def show(self):
15+
p = self
16+
while p != None:
17+
print p.val
18+
p = p.next
19+
20+
def toArray(self):
21+
node = self
22+
r = list()
23+
while node != None:
24+
r.append(node.val)
25+
node = node.next
26+
return r
27+
28+
# @param [in] list
29+
# @return header ListNode
30+
def makeList(array):
31+
if array == None:
32+
return None
33+
34+
p = head = ListNode(array[0])
35+
for i in range(1, len(array)):
36+
p = p.addNext(array[i])
37+
return head
38+
39+
def test():
40+
print "this is the test of ListNode."
41+
42+
if __name__ == '__main__':
43+
test()

0 commit comments

Comments
 (0)