Skip to content

Commit 11d6841

Browse files
committed
day7:intersection of lists
1 parent b585a9e commit 11d6841

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
| :------------: | :----------: |
99
| Total C++ Problems | 188 |
1010
| Total Python Problems | 11 |
11-
| Current Daily Streak| 6 |
11+
| Current Daily Streak| 7 |
1212
| Last Streak | 06/20/2019 - 06/21/2019|
13-
| Current Streak | 06/23/2019 - 06/28/2019|
13+
| Current Streak | 06/23/2019 - 06/29/2019|
1414

1515
</center>
1616

@@ -29,7 +29,7 @@
2929
| Print middle node of linkedlist without iterating twice. | [printMiddleNode.cpp](linked_list_problems/printMiddleNode.cpp) | | Detecting and removing a cycle in linkedlist.| [floyedCycleDetection.cpp](linked_list_problems/floyedCycleDetection.cpp)|
3030
| Determine if a linked list is a pallindrome. | [listPallindrome.cpp](linked_list_problems/listPallindrome.cpp) |
3131
| Insert data in a sorted linked list.|[insertInASortedLinkedList.cpp](linked_list_problems/insertInASortedLinkedList.cpp) |
32-
| Determine the intersection(merging) point of two given linked list.| [findIntersectionPointOfLists.cpp](linked_list_problems/findIntersectionPointOfLists.cpp)|
32+
| Determine the intersection(merging) point of two given linked list.| [findIntersectionPointOfLists.cpp](linked_list_problems/findIntersectionPointOfLists.cpp), [intersection_of_lists.py](linked_list_problems/intersection_of_lists.py)|
3333
| Clone a linkedlist which has next and an random pointer, Space Complexity - O(1). | [cloneListWithRandomPtr.cpp](linked_list_problems/cloneListWithRandomPtr.cpp), [clone_list_with_random_ptr.py](linked_list_problems/clone_list_with_random_ptr.py)|
3434
| Given a sorted linked list with duplicates, remove duplicates in one iteration. | [removeDuplicatesFromSortedList.cpp](linked_list_problems/removeDuplicatesFromSortedList.cpp)|
3535
| Using Floyd's cycle finding algorithm, detect if a linkedlist contain cycle, if it does contain cycle, remove the loop | [floyedCycleDetection.cpp](linked_list_problems/floyedCycleDetection.cpp) |
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
Given two linked lists, they both merge at some point.
3+
Find out the merging point
4+
5+
1-->2-->3-->4-->5
6+
^
7+
|
8+
7-->8-->9
9+
10+
List 1 and List 2 intereset at 3
11+
"""
12+
13+
class Node:
14+
def __init__(self, data):
15+
"""Represents a linked list node."""
16+
self.next = None
17+
self.data = data
18+
19+
def print(self):
20+
"""Prints the node's data with its address."""
21+
print(self.data, '(', hex(id(self)), ')', end=' ')
22+
23+
def len(head):
24+
"""Length of the list."""
25+
count = 0
26+
itr = head
27+
while itr:
28+
count += 1
29+
itr = itr.next
30+
return count
31+
32+
def intersect(head1, head2):
33+
len1 = len(head1)
34+
len2 = len(head2)
35+
smaller = head1 if len1 < len2 else head2
36+
larger = head1 if len1 > len2 else head2
37+
38+
diff = abs(len1-len2)
39+
i = 0
40+
while larger and i < diff:
41+
i += 1
42+
larger = larger.next
43+
44+
itr1 = smaller
45+
itr2 = larger
46+
while itr1 and itr2:
47+
if itr1 == itr2:
48+
return itr1
49+
itr1 = itr1.next
50+
itr2 = itr2.next
51+
return None
52+
53+
def print_list(node):
54+
while node:
55+
node.print()
56+
print('-->', end=' ')
57+
node = node.next
58+
print('None')
59+
60+
61+
if __name__ == "__main__":
62+
head = Node(1)
63+
head.next = Node(2)
64+
head.next.next = Node(3)
65+
head.next.next.next = Node(4)
66+
head.next.next.next.next = Node(5)
67+
68+
print("List 1:")
69+
print_list(head)
70+
71+
head2 = Node(7)
72+
head2.next = Node(8)
73+
head2.next.next = Node(9)
74+
head2.next.next.next = head.next.next
75+
76+
print("List 2:")
77+
print_list(head2)
78+
79+
print("Intersection of list1 and list2:")
80+
intersect(head, head2).print()
81+

0 commit comments

Comments
 (0)