today we are going to learn how to merge a linked list into another linked list. like we have two linked lists as you see in the images given below.

so we want to merge the second list into the first list. so first we need to find the reference of the last node of the first linked list.

and then we store the second linked list's first node's reference into the linked part of the last of the first linked list.
a second linked list has three nodes.

so we want to merge the second list into the first list. so first we need to find the reference of the last node of the first linked list.

and then we store the second linked list's first node's reference into the linked part of the last of the first linked list.
Python program for merging one list into another linked list.
class Node: def __init__(self,value): self.info = value self.link = None class SingleLinkedList: def __init__(self): self.start = None def display_list(self): if self.start is None: print("List is empty") return else: print("List is : ") p = self.start while p is not None: print(p.info, " ", end='') p = p.link print() def insert_at_end(self, data): temp = Node(data) if self.start is None: self.start = temp return p = self.start while p.link is not None: p = p.link p.link = temp def create_list(self): n = int(input("Enter the numer of nodes : ")) if n == 0: return for i in range(n): data = int(input("Enter the element to be inserted : ")) self.insert_at_end(data) def concatenate(self, list2): if self.start is None: self.start = list2.start return if list2.start is None: return p = self.start while p.link is not None: p = p.link p.link = list2.start ############################################################ list1 = SingleLinkedList() list2 = SingleLinkedList() print("Enter first list :- ") list1.create_list() print("Enter second list :- ") list2.create_list() print("First ") list1.display_list() print("Second ") list2.display_list() list1.concatenate(list2) print("First ") list1.display_list()
Also, read these posts
- What are Data Structures and algorithms
- Algorithm design and analysis
- Classification of algorithms
- How to calculate the running time of an algorithm.
- Worst Average and Best-case analysis of the algorithm.
- Big o notation
- Big o notation examples
- Linked List in Data Structures
- Traversing in Linked list
- Operations on the linked list
- Insertion in the linked list
- Deletion in a linked list
- Reversing a linked list
- Sorting a linked list
- Find and remove the loop in the linked list
- Doubly linked list
- Insertion in the doubly linked list
- Deletion in the doubly linked list
- Reversing a doubly linked list
- Circular linked list
- Insertion in the circular linked list
- Deletion in the circular linked list
- Header linked list
- Sorted linked list
- Stack in data structures
- Queue in data structures
- Circular queue
- Dequeue in the data structure
- Priority queue
- Polish notation
- Tree in the data structure
- Binary tree
- Array representation of the binary tree
- linked representation of a binary tree
- Traversing in the binary tree
- Inorder traversal in the binary tree
- Preorder traversal in the binary tree
- Postorder traversal in the binary tree
- Level order traversal in the binary tree
- Binary search tree
- Insertion in the binary search tree
- Deletion in the binary search tree
- Heap in data structures
0 Comments