This is a Python program to add the corresponding elements of two linked lists.
The program creates two linked lists using data items input from the user and creates a linked list consisting of the sum of data items of the two lists.
1. Create a class Node with instance variables data and next.
2. Create a class LinkedList with instance variables head and last_node.
3. The variable head points to the first element in the linked list while last_node points to the last.
4. Define methods append and display inside the class LinkedList to append data and display the linked list respectively.
5. Define a function add_linked_lists which takes two linked lists as arguments.
6. The function add_linked_lists returns a linked list which has its elements as the sum of the data items of the two lists passed to it. If one list is shorter, then the rest of the elements are appended from the longer list.
7. Create two instances of LinkedList and append data to it.
8. Create the sum linked list and display it.
Here is the source code of a Python program to add the corresponding elements of two linked lists. The program output is shown below.
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None self.last_node = None def append(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 display(self): current = self.head while current is not None: print(current.data, end = ' ') current = current.next def add_linked_lists(llist1, llist2): sum_llist = LinkedList() current1 = llist1.head current2 = llist2.head while (current1 and current2): sum = current1.data + current2.data sum_llist.append(sum) current1 = current1.next current2 = current2.next if current1 is None: while current2: sum_llist.append(current2.data) current2 = current2.next else: while current1: sum_llist.append(current1.data) current1 = current1.next return sum_llist llist1 = LinkedList() llist2 = LinkedList() data_list = input('Please enter the elements in the first linked list: ').split() for data in data_list: llist1.append(int(data)) data_list = input('Please enter the elements in the second linked list: ').split() for data in data_list: llist2.append(int(data)) sum_llist = add_linked_lists(llist1, llist2) print('The sum linked list: ', end = '') sum_llist.display()
1. Two instances of LinkedList are created.
2. The user is prompted to enter the data items for the two lists.
3. The function add_linked_lists is called to create the sum linked list.
4. This list is then displayed using the display method.
Case 1: Please enter the elements in the first linked list: 1 9 10 15 Please enter the elements in the second linked list: 2 5 4 The sum linked list: 3 14 14 15 Case 2: Please enter the elements in the first linked list: 3 4 5 Please enter the elements in the second linked list: The sum linked list: 3 4 5 Case 3: Please enter the elements in the first linked list: 7 10 Please enter the elements in the second linked list: 6 5 4 3 2 The sum linked list: 13 15 4 3 2
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Python Internship
- Practice Programming MCQs
- Buy Python Books
- Apply for Programming Internship
- Buy Information Technology Books