
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Construct Maximum Sum Linked List from Two Sorted Lists in Python
Suppose we have two sorted linked lists, we have to make a linked list that consists of largest sum path from start node to end node. The final list may consist of nodes from both input lists.
When we are creating the result list, we may switch to the other input list only for the point of intersection (two node with the same value in the lists). We have to solve it using constant amount of extra space.
So, if the input is like [6,8,35,95,115,125], [5,8,17,37,95,105,125,135], then the output will be [6,8,17,37,95,115,125,135]
To solve this, we will follow these steps −
result := None
previous1 := a, current1 := a
previous2 := b, current2 := b
-
while current1 is not same as None or current2 is not same as None, do
res1 := 0, res2 := 0
-
while current1 and current2 are not null and data of current1 is not same as data of current2, do
-
if data of current1 < data of current2, then
res1 := res1 + data of current1
current1 := next of current1
-
otherwise,
res2 := res2 + data of current2
current2 := next of current2
-
-
if current1 is null, then
-
while current2 is not null, do
res2 := res2 + data of current2
current2 := next of current2
-
-
if current2 is null, then
-
while current1 is not null, do
res1 := res1 + data of current1
current1 := next of current1
-
-
if previous1 is same as a and previous2 is same as b, then
result := previous1 when (res1 > res2) otherwise previous2
-
otherwise,
-
if res1 > res2, then
next of previous2 := next of previous1
-
otherwise,
next of previous1 := next of previous2
-
previous1 := current1
previous2 := current2
-
if current1 is not null, then
current1 := next of current1
-
if current2 is not null, then
current2 := next of current2
display the content of result.
Example
Let us see the following implementation to get better understanding −
class LinkedList(object): def __init__(self, data_set = []): self.head = None if len(data_set) > 0: for item in data_set: self.insert_node(item) class ListNode(object): def __init__(self, d): self.data = d self.next = None def insert_node(self, new_data): new_node = self.ListNode(new_data) new_node.next = self.head self.head = new_node def find_max_sum_list(self, a, b): result = None previous1 = a current1 = a previous2 = b current2 = b while current1 != None or current2 != None: res1 = 0 res2 = 0 while current1 != None and current2 != None and current1.data != current2.data: if current1.data < current2.data: res1 += current1.data current1 = current1.next else: res2 += current2.data current2 = current2.next if current1 == None: while current2 != None: res2 += current2.data current2 = current2.next if current2 == None: while current1 != None: res1 += current1.data current1 = current1.next if previous1 == a and previous2 == b: result = previous1 if (res1 > res2) else previous2 else: if res1 > res2: previous2.next = previous1.next else: previous1.next = previous2.next previous1 = current1 previous2 = current2 if current1 != None: current1 = current1.next if current2 != None: current2 = current2.next while result != None: print(result.data, end = ' ') result = result.next my_list1 = LinkedList([125,115,95,35,8,6]) my_list2 = LinkedList([135,125,105,95,37,17,8,5]) my_list1.find_max_sum_list(my_list1.head, my_list2.head)
Input
[125,115,95,35,8,6], [135,125,105,95,37,17,8,5]
Output
6 8 17 37 95 115 125 135