Skip to content

Commit 71f1ae8

Browse files
authored
Merge pull request #14 from nickolasteixeira/chapter2
Chapter2 - more singly linked list problems
2 parents e617845 + 19e8091 commit 71f1ae8

File tree

9 files changed

+835
-0
lines changed

9 files changed

+835
-0
lines changed

Python/chapter02/2.1 - Remove Dups/Nick.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/usr/bin/python3
2+
'''
3+
Remove Dups! Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed?
4+
'''
25
import unittest
36

47

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/python3
2+
3+
import unittest
4+
5+
6+
class Node:
7+
def __init__(self, data, next=None):
8+
self.data = data
9+
self.next = next
10+
11+
def __str__(self):
12+
return str(self.data)
13+
14+
15+
class SinglyLinkedList:
16+
def __init__(self):
17+
self.head = None
18+
19+
def addNode(self, data):
20+
node = Node(data)
21+
if self.head is None:
22+
self.head = node
23+
else:
24+
temp = self.head
25+
while temp.next:
26+
temp = temp.next
27+
temp.next = node
28+
print('Node added to LL--->', data)
29+
30+
def printList(self):
31+
if self.head is None:
32+
print('No LinkedList to print')
33+
return
34+
35+
temp = self.head
36+
while temp:
37+
print(temp.data)
38+
temp = temp.next
39+
40+
def partition(self, part):
41+
'''However, in a linked list, the situation is much easier. Rather than shifting and swapping elements, we can actually create two different linked lists: one for elements less than x, and one for elements greater than or equal to x.'''
42+
if self.head is None:
43+
print('No LinkedList to print')
44+
return
45+
46+
temp = self.head
47+
before = SinglyLinkedList()
48+
after = SinglyLinkedList()
49+
while temp.next:
50+
if temp.data >= part:
51+
after.addNode(temp.data)
52+
else:
53+
before.addNode(temp.data)
54+
temp = temp.next
55+
56+
# combine the two linked lists
57+
self.head = before.head
58+
temp = before.head
59+
while temp.next:
60+
temp = temp.next
61+
temp.next = after.head
62+
63+
64+
class Test(unittest.TestCase):
65+
66+
@classmethod
67+
def setUpClass(cls):
68+
cls.l = SinglyLinkedList()
69+
cls.l.printList()
70+
cls.l.addNode(3)
71+
cls.l.addNode(20)
72+
cls.l.addNode(21)
73+
cls.l.addNode(5)
74+
cls.l.addNode(8)
75+
cls.l.addNode(5)
76+
cls.l.addNode(10)
77+
cls.l.addNode(2)
78+
cls.l.addNode(1)
79+
cls.l.printList()
80+
81+
@classmethod
82+
def tearDownClass(cls):
83+
pass
84+
85+
def test1(self):
86+
self.l.partition(5)
87+
self.l.printList()
88+
89+
90+
if __name__ == '__main__':
91+
unittest.main()
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/python3
2+
3+
import unittest
4+
5+
6+
class Node:
7+
def __init__(self, data, next=None):
8+
self.data = data
9+
self.next = next
10+
11+
12+
class SinglyLinkedList:
13+
def __init__(self):
14+
self.head = None
15+
16+
def addNode(self, data):
17+
node = Node(data)
18+
if not self.head:
19+
self.head = node
20+
else:
21+
temp = self.head
22+
while temp.next:
23+
temp = temp.next
24+
temp.next = node
25+
print("Node added -->", data)
26+
27+
def printList(self):
28+
temp = self.head
29+
while temp:
30+
print(temp.data)
31+
temp = temp.next
32+
33+
def findLength(self, ll):
34+
if not ll.head:
35+
print('No list to traverse')
36+
return
37+
temp = ll.head
38+
count = 0
39+
while temp:
40+
count += 1
41+
temp = temp.next
42+
return count
43+
44+
def turnIntoNum(self, ll, direction=True):
45+
'''if direction = true, then sum in place
46+
if direction = false, then reverse the ll to sum'''
47+
48+
if not ll.head:
49+
print('No list to traverse')
50+
return
51+
num = 0
52+
length = self.findLength(ll)
53+
if direction:
54+
power = length - 1
55+
n = 0
56+
temp = ll.head
57+
while n < length:
58+
if direction:
59+
num += temp.data * (10 ** power)
60+
power -= 1
61+
else:
62+
num += temp.data * (10 ** n)
63+
temp = temp.next
64+
n += 1
65+
print("Number to sum", num)
66+
return num
67+
68+
def addTwoLinkedLists(self, l1, l2, direction=True):
69+
num1 = self.turnIntoNum(l1, direction)
70+
num2 = self.turnIntoNum(l2, direction)
71+
if num1 and num2:
72+
return num1 + num2
73+
else:
74+
print('No numbers to return')
75+
return
76+
77+
78+
class Test(unittest.TestCase):
79+
@classmethod
80+
def setUpClass(cls):
81+
cls.l = SinglyLinkedList()
82+
cls.l.addNode(7)
83+
cls.l.addNode(1)
84+
cls.l.addNode(6)
85+
86+
cls.k = SinglyLinkedList()
87+
cls.k.addNode(5)
88+
cls.k.addNode(9)
89+
cls.k.addNode(2)
90+
91+
@classmethod
92+
def tearDownClass(cls):
93+
pass
94+
95+
def test1(self):
96+
sumReverse = self.l.addTwoLinkedLists(self.l, self.k, False)
97+
print('Sum ->', sumReverse)
98+
sumLL = self.l.addTwoLinkedLists(self.l, self.k)
99+
print('Sum ->', sumLL)
100+
101+
102+
if __name__ == '__main__':
103+
unittest.main()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/python3
2+
3+
import unittest
4+
5+
6+
class Node:
7+
def __init__(self, data, next=None):
8+
self.data = data
9+
self.next = next
10+
11+
12+
class SinglyLinkedList:
13+
def __init__(self):
14+
self.head = None
15+
16+
def appendNode(self, data):
17+
node = Node(data)
18+
if not self.head:
19+
self.head = node
20+
else:
21+
temp = self.head
22+
while temp.next:
23+
temp = temp.next
24+
temp.next = node
25+
print('Node added to LL-->', data)
26+
27+
def isPalindrome(self):
28+
if not self.head:
29+
print('No LL to traverse')
30+
return
31+
32+
pali = []
33+
temp = self.head
34+
while temp:
35+
pali.append(temp.data)
36+
temp = temp.next
37+
38+
strpali = ''.join(pali)
39+
return strpali == strpali[::-1]
40+
41+
42+
class Test(unittest.TestCase):
43+
@classmethod
44+
def setUpClass(cls):
45+
cls.l = SinglyLinkedList()
46+
cls.l.appendNode('r')
47+
cls.l.appendNode('a')
48+
cls.l.appendNode('c')
49+
cls.l.appendNode('e')
50+
cls.l.appendNode('c')
51+
cls.l.appendNode('a')
52+
cls.l.appendNode('r')
53+
54+
cls.k = SinglyLinkedList()
55+
cls.k.appendNode('n')
56+
cls.k.appendNode('i')
57+
cls.k.appendNode('c')
58+
cls.k.appendNode('k')
59+
60+
@classmethod
61+
def tearDownClass(cls):
62+
pass
63+
64+
def test1(self):
65+
self.assertTrue(self.l.isPalindrome())
66+
self.assertFalse(self.k.isPalindrome())
67+
68+
69+
if __name__ == '__main__':
70+
unittest.main()

0 commit comments

Comments
 (0)