Skip to content

Commit 562ae07

Browse files
intersection of nodes
1 parent c0cf1f0 commit 562ae07

File tree

1 file changed

+131
-0
lines changed
  • Python/chapter02/2.7 - Intersection

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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, next=None):
17+
node = Node(data, next)
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 printMemoryValues(self):
28+
temp = self.head
29+
while temp:
30+
print('Memory address values: ', hex(id(temp)), ' Values: ', temp.data)
31+
temp = temp.next
32+
33+
def findNode(self, value):
34+
if not self.head:
35+
print('No list to iterate')
36+
return
37+
38+
temp = self.head
39+
while temp:
40+
if temp.data == value:
41+
return temp
42+
temp = temp.next
43+
return None
44+
45+
def findLongest(self, l1, l2):
46+
if not self.head:
47+
print('No list to iterate')
48+
return None
49+
50+
l1count, l2count = 0, 0
51+
temp1, temp2 = l1.head, l2.head
52+
while temp1:
53+
l1count += 1
54+
temp1 = temp1.next
55+
56+
while temp2:
57+
l2count += 1
58+
temp2 = temp2.next
59+
60+
if l1count > l2count:
61+
longest, shortest = l1, l2
62+
else:
63+
longest, shortest = l2, l1
64+
return abs(l1count - l2count), longest, shortest
65+
66+
67+
def intersection(self, l1, l2):
68+
if not l1.head or not l2.head:
69+
print('No list to iterate -> intersection')
70+
return None
71+
72+
diff, longest, shortest = self.findLongest(l1, l2)
73+
temp = longest.head
74+
for x in range(diff):
75+
temp = temp.next
76+
77+
temp2 = shortest.head
78+
while temp:
79+
if temp == temp2:
80+
return temp
81+
temp = temp.next
82+
temp2 = temp2.next
83+
84+
return None
85+
86+
87+
class Test(unittest.TestCase):
88+
@classmethod
89+
def setUpClass(cls):
90+
cls.l = SinglyLinkedList()
91+
cls.l.appendNode(3)
92+
cls.l.appendNode(1)
93+
cls.l.appendNode(5)
94+
cls.l.appendNode(9)
95+
cls.l.appendNode(7)
96+
cls.l.appendNode(2)
97+
cls.l.appendNode(1)
98+
cls.l.printMemoryValues()
99+
100+
cls.k = SinglyLinkedList()
101+
cls.k.appendNode(4)
102+
node = cls.l.findNode(7)
103+
cls.k.appendNode(6, node)
104+
cls.k.printMemoryValues()
105+
106+
107+
cls.p = SinglyLinkedList()
108+
cls.p.appendNode(1)
109+
cls.p.appendNode(2)
110+
cls.p.appendNode(5)
111+
cls.p.appendNode(1000)
112+
cls.p.appendNode(-22)
113+
cls.p.printMemoryValues()
114+
@classmethod
115+
def tearDownClass(cls):
116+
pass
117+
118+
119+
def printStatement(self, interNode):
120+
if interNode:
121+
print('Nodes intersect at address', hex(id(interNode)), ' Value: ',interNode.data)
122+
else:
123+
print('Nodes do not intersect')
124+
def test1(self):
125+
interNode = self.l.intersection(self.l, self.k)
126+
interNode2 = self.l.intersection(self.l, self.p)
127+
self.printStatement(interNode)
128+
self.printStatement(interNode2)
129+
130+
if __name__ == '__main__':
131+
unittest.main()

0 commit comments

Comments
 (0)