
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes 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
- Related Articles
- Find count of common nodes in two Doubly Linked Lists in C++
- Merge two sorted linked lists using C++.
- Find the common nodes in two singly linked list in C++
- Intersection of Two Linked Lists in Python
- Sum of the nodes of a Circular Linked List in C++
- Sum of the alternate nodes of linked list in C++
- Sum of smaller elements of nodes in a linked list in C++
- Merge K sorted linked lists in Java
- Sum of the nodes of a Singly Linked List in C Program
- Program to interleave list elements from two linked lists in Python
- Program to swap nodes in a linked list in Python
- Remove Zero Sum Consecutive Nodes from Linked List in C++
- Program to reverse inner nodes of a linked list in python
- Find sum of even and odd nodes in a linked list in C++
- Program to find linked list intersection from two linked list in Python
