Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes in Python
Given two sorted linked lists, we need to construct a new linked list that represents the maximum sum path from start to end. We can switch between lists only at common nodes (nodes with the same value).
The algorithm compares segments between common nodes and chooses the path with the higher sum. This creates an optimal path that maximizes the total sum.
Example
If we have lists [6,8,35,95,115,125] and [5,8,17,37,95,105,125,135], the common nodes are 8, 95, and 125. We compare segment sums between these points and choose the better path ?
Algorithm Steps
The solution follows these key steps ?
Initialize pointers for both linked lists
For each segment between common nodes, calculate the sum
Choose the segment with higher sum and link it to the result
Continue until both lists are exhausted
Implementation
class ListNode:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self, data_set=[]):
self.head = None
if len(data_set) > 0:
# Insert in reverse order to maintain sorted order
for item in reversed(data_set):
self.insert_node(item)
def insert_node(self, new_data):
new_node = 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
# Calculate sum until common node or end
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
# Handle remaining nodes in first list
if current1 == None:
while current2 != None:
res2 += current2.data
current2 = current2.next
# Handle remaining nodes in second list
if current2 == None:
while current1 != None:
res1 += current1.data
current1 = current1.next
# Choose the better path
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
# Move to next segment
previous1 = current1
previous2 = current2
if current1 != None:
current1 = current1.next
if current2 != None:
current2 = current2.next
# Display result
while result != None:
print(result.data, end=' ')
result = result.next
print()
# Create test lists
my_list1 = LinkedList([6, 8, 35, 95, 115, 125])
my_list2 = LinkedList([5, 8, 17, 37, 95, 105, 125, 135])
my_list1.find_max_sum_list(my_list1.head, my_list2.head)
The output shows the optimal path ?
6 8 17 37 95 115 125 135
How It Works
The algorithm identifies segments between common nodes:
Segment 1: [6] vs [5] ? Choose [6] (sum = 6 > 5)
Segment 2: [35] vs [17,37] ? Choose [17,37] (sum = 54 > 35)
Segment 3: [115] vs [105] ? Choose [115] (sum = 115 > 105)
Segment 4: [] vs [135] ? Choose [135]
The final path combines the optimal segments: 6 ? 8 ? 17 ? 37 ? 95 ? 115 ? 125 ? 135.
Conclusion
This algorithm efficiently constructs the maximum sum path by comparing segment sums between common nodes. It uses constant extra space and produces an optimal solution by selecting the better path at each decision point.
