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
Selected Reading
Python Program to Add Corresponding Positioned Elements of 2 Linked Lists
When working with linked lists, you may need to add corresponding elements from two lists at the same positions. This involves traversing both lists simultaneously and creating a new linked list with the sum of elements at each position.
Below is a demonstration for the same ?
Node and LinkedList Classes
First, we define the basic structure for nodes and linked lists ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList_structure:
def __init__(self):
self.head = None
self.last_node = None
def add_vals(self, data):
if self.last_node is None:
self.head = Node(data)
self.last_node = self.head
else:
self.last_node.next = Node(data)
self.last_node = self.last_node.next
def print_it(self):
curr = self.head
while curr is not None:
print(curr.data)
curr = curr.next
Adding Corresponding Elements
The add_linked_list function traverses both lists simultaneously and adds corresponding elements ?
def add_linked_list(my_list_1, my_list_2):
sum_list = LinkedList_structure()
curr_1 = my_list_1.head
curr_2 = my_list_2.head
# Add corresponding elements while both lists have nodes
while (curr_1 and curr_2):
sum_val = curr_1.data + curr_2.data
sum_list.add_vals(sum_val)
curr_1 = curr_1.next
curr_2 = curr_2.next
# Add remaining elements from the longer list
if curr_1 is None:
while curr_2:
sum_list.add_vals(curr_2.data)
curr_2 = curr_2.next
else:
while curr_1:
sum_list.add_vals(curr_1.data)
curr_1 = curr_1.next
return sum_list
Complete Example
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList_structure:
def __init__(self):
self.head = None
self.last_node = None
def add_vals(self, data):
if self.last_node is None:
self.head = Node(data)
self.last_node = self.head
else:
self.last_node.next = Node(data)
self.last_node = self.last_node.next
def print_it(self):
curr = self.head
while curr is not None:
print(curr.data)
curr = curr.next
def add_linked_list(my_list_1, my_list_2):
sum_list = LinkedList_structure()
curr_1 = my_list_1.head
curr_2 = my_list_2.head
while (curr_1 and curr_2):
sum_val = curr_1.data + curr_2.data
sum_list.add_vals(sum_val)
curr_1 = curr_1.next
curr_2 = curr_2.next
if curr_1 is None:
while curr_2:
sum_list.add_vals(curr_2.data)
curr_2 = curr_2.next
else:
while curr_1:
sum_list.add_vals(curr_1.data)
curr_1 = curr_1.next
return sum_list
# Create two linked lists
my_list_1 = LinkedList_structure()
my_list_2 = LinkedList_structure()
# Add elements to first list: [56, 34, 78, 99, 54, 11]
first_elements = [56, 34, 78, 99, 54, 11]
for elem in first_elements:
my_list_1.add_vals(elem)
# Add elements to second list: [23, 56, 99, 0, 122, 344]
second_elements = [23, 56, 99, 0, 122, 344]
for elem in second_elements:
my_list_2.add_vals(elem)
# Add corresponding elements
sum_list = add_linked_list(my_list_1, my_list_2)
print('The sum of elements in the linked list is:')
sum_list.print_it()
The sum of elements in the linked list is: 79 90 177 99 176 355
How It Works
- Node class: Represents individual nodes with data and next pointer
- LinkedList_structure class: Manages the linked list with head and last_node pointers
- add_vals method: Adds new elements to the end of the list
- print_it method: Traverses and prints all elements
- add_linked_list function: Adds corresponding elements from both lists and handles different lengths
Key Points
- The algorithm handles lists of different lengths by appending remaining elements
- Two pointers traverse both lists simultaneously
- A new linked list stores the sum results
- Time complexity: O(max(m, n)) where m and n are the lengths of the lists
Conclusion
This program demonstrates how to add corresponding elements from two linked lists using simultaneous traversal. The algorithm handles lists of different lengths by appending remaining elements from the longer list.
Advertisements
