Skip to content

Commit 79a931c

Browse files
2.7 done
1 parent 6b8ed65 commit 79a931c

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
"""
2+
Intersection: Given two (singly) linked lists, determine if the two lists
3+
intersect. Return the inter­secting node. Note that the intersection is defined
4+
based on reference, not value.That is, if the kth node of the first linked list
5+
is the exact same node (by reference) as the jth node of the second linked list,
6+
then they are intersecting.
7+
Hints:#20, #45, #55, #65, #76, #93, #111, #120, #129
8+
9+
10+
Had a really hard time with this question:
11+
https://www.youtube.com/watch?v=gMeDsyEXnbM
12+
13+
"""
14+
15+
16+
17+
class Node:
18+
#Singly link list
19+
def __init__(self,data = None):
20+
self.data = data
21+
self.next = None
22+
23+
class linklist:
24+
#linkList class
25+
def __init__(self):
26+
self.head = None
27+
self.size = 0
28+
29+
def getSize(self):
30+
return self.size
31+
32+
def push(self,data):
33+
node = Node(data)# create a new node
34+
35+
if self.head == None: #check to see if the head node is empty
36+
self.head = node # If its empty add it to the new node
37+
self.size = 1
38+
return
39+
#if the head of the linklist is filled
40+
41+
current = self.head
42+
while current.next is not None:#Check the current postion is empty
43+
#Move to the next line if nothing is there
44+
current = current.next
45+
46+
47+
current.next = node #point self.head to a new node
48+
self.size+=1
49+
50+
def lenght(self):
51+
#note the count doesn't start at zero
52+
cur = self.head
53+
counter = 0
54+
while cur is not None:
55+
counter+=1
56+
cur = cur.next
57+
print('Linklist len: '+str(counter))
58+
return counter
59+
60+
def printList(self):
61+
curr = self.head
62+
elem = []
63+
64+
while(curr != None):
65+
elem.append(curr.data)
66+
curr = curr.next
67+
print(elem)
68+
#1->2->3
69+
def remove_node(self,data):
70+
#1->2->3
71+
curr = self.head
72+
if curr is not None and curr.data == data:
73+
self.head = curr.next
74+
curr = None
75+
#look for the node that has the data we are looking for
76+
while curr is not None:
77+
if curr.data == data:
78+
break
79+
prev = curr
80+
curr = curr.next
81+
82+
#if data isn't found just reutrn
83+
if(curr == None):
84+
return
85+
86+
#allow us to unlink the nodes
87+
prev.next = curr.next
88+
curr = None
89+
90+
"""
91+
def make_next(self,llist,num):
92+
curr_llist = llist.head
93+
curr = self.head
94+
95+
size_ = self.size - llist.size
96+
print(size_)
97+
size = 0
98+
if self.size == llist.size:
99+
while curr and curr_llist is not None:
100+
if curr.data == num:
101+
curr_llist.next = curr
102+
103+
curr = curr.next
104+
curr_llist = curr_llist.next
105+
106+
while curr is not None:
107+
if size != size_:
108+
size += 1
109+
curr = curr.next
110+
while curr and curr_llist is not None:
111+
if curr.data == num:
112+
curr_llist.next = curr
113+
114+
curr = curr.next
115+
curr_llist = curr_llist.next
116+
117+
self.head = list_A.head
118+
head_A = list_A.head
119+
while head_A.next:
120+
head_A = head_A.next
121+
head_A.next = list_B.head
122+
123+
def CombineLlist(self,list_A,list_B):
124+
list_B = list_B.head
125+
head_A = self.head
126+
while head_A is not None:
127+
head_A = head_A.next
128+
head_A.next = list_B.head
129+
"""
130+
131+
132+
def intersection(listA,ListB):
133+
headA = listA.head
134+
headB = ListB.head
135+
136+
if not headA or not headB:
137+
return None
138+
139+
a = headA
140+
b = headB
141+
142+
143+
while a != b and a!= None and b != None:
144+
if not a:
145+
a = headB
146+
else:
147+
148+
a = a.next
149+
150+
if not b:
151+
b = headA
152+
else:
153+
154+
b = b.next
155+
return a.data
156+
157+
158+
159+
160+
161+
162+
# 1->2->3->4->5->6->7
163+
# 3->5->6->7
164+
llist_1 = linklist()
165+
llist_2 = linklist()
166+
llist_1.push(1)
167+
llist_1.push(3)
168+
llist_1.push(5)
169+
llist_1.push(7)
170+
llist_1.push(9)
171+
llist_1.push(11)
172+
173+
llist_2.push(2)
174+
llist_2.push(4)
175+
llist_2.push(9)
176+
llist_2.push(11)
177+
178+
print(intersection(llist_1,llist_2))

Python/chapter02/2.7 - Intersection/solution.py

Whitespace-only changes.

0 commit comments

Comments
 (0)