Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a6550eb

Browse files
Create MergeNodeLinkedList
1 parent 872b412 commit a6550eb

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

‎MergeNodeLinkedList

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/bin/python3
2+
3+
import math
4+
import os
5+
import random
6+
import re
7+
import sys
8+
9+
class SinglyLinkedListNode:
10+
def __init__(self, node_data):
11+
self.data = node_data
12+
self.next = None
13+
14+
class SinglyLinkedList:
15+
def __init__(self):
16+
self.head = None
17+
self.tail = None
18+
19+
def insert_node(self, node_data):
20+
node = SinglyLinkedListNode(node_data)
21+
22+
if not self.head:
23+
self.head = node
24+
else:
25+
self.tail.next = node
26+
27+
28+
self.tail = node
29+
30+
def print_singly_linked_list(node, sep, fptr):
31+
while node:
32+
fptr.write(str(node.data))
33+
34+
node = node.next
35+
36+
if node:
37+
fptr.write(sep)
38+
39+
# Complete the findMergeNode function below.
40+
41+
#
42+
# For your reference:
43+
#
44+
# SinglyLinkedListNode:
45+
# int data
46+
# SinglyLinkedListNode next
47+
#
48+
#
49+
def len_list(head1, head2):
50+
current1, count1 = head1, 0
51+
current2, count2 = head2, 0
52+
while current1:
53+
count1 += 1
54+
print("current1:", current1.data, end=',')
55+
current1 = current1.next
56+
print()
57+
while current2:
58+
count2 += 1
59+
print("current2:", current2.data, end=',')
60+
current2 = current2.next
61+
return (count1, count2)
62+
63+
def findMergeNode(head1, head2):
64+
# O(n) time complexity to calculate the length of linked list
65+
# Also, traversing the linked list in above function for debugging
66+
count1, count2 = len_list(head1, head2)
67+
print("\n", count1, count2)
68+
# Assuming linked list by current1 is smaller in length compared to that by current2
69+
# Thus, calculating diff by count2-count1
70+
current1, current2 = head1, head2
71+
d = count2 - count1
72+
# if count1>count2, making diff positive, swapping pointers value to ensure the
73+
# largest linked list is always denoted by pointer current2.
74+
if count1 > count2:
75+
d = count1 - count2
76+
current1, current2 = current2, current1
77+
print("d:{}".format(d))
78+
# O(j) time to reach the equidistant point from merge node
79+
for i in range(d):
80+
print("current2, before iter:{}".format(current2.data), end=',')
81+
current2 = current2.next
82+
print("\ncurrent2, after iter:{}".format(current2.data), end=',')
83+
print()
84+
# pointers are at equidistant node from the merge node, iterating until we get the
85+
# same node in both the pointers.
86+
while current1 and current2:
87+
print("current1:{}, current2:{}".format(current1.data, current2.data))
88+
if current1 == current2:
89+
return current1.data
90+
current1 = current1.next
91+
current2 = current2.next
92+
"""
93+
Takes Time=O(n+m), Space=O(n)
94+
dic = {}
95+
current = head1
96+
while current:
97+
dic[current] = 1
98+
current = current.next
99+
current = head2
100+
while current:
101+
if current in dic:
102+
return current.data
103+
current = current.next
104+
"""

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /