Given two linked list of equal sizes, the task is to create new linked list using those linked lists where at every step, the maximum of the two elements from both the linked lists is chosen and the other is skipped.
list1 = 5 -> 2 -> 3 -> 8 -> NULL
list2 = 1 -> 7 -> 4 -> 5 -> NULL
Output: 5 -> 7 -> 4 -> 8 -> NULL
list1 = 2 -> 8 -> 9 -> 3 -> NULL
list2 = 5 -> 3 -> 6 -> 4 -> NULL
Output: 5 -> 8 -> 9 -> 4 -> NULL
Approach: We traverse both the linked list at the same time and compare node from both the lists. The node which is greater among them, will be added to the new linked list. We do this for each node and then print the nodes of the generated linked list.
Below is the implementation of the above approach:
5 -> 7 -> 4 -> 8 -> NULL
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Create new linked list from two given linked list with greater element at each node
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Insert a whole linked list into other at k-th position
- First common element in two linked lists
- Rotate the sub-list of a linked list from position M to N to the right by K places
- Make a loop at k-th position in a linked list
- Delete a Linked List node at a given position
- Insert N elements in a Linked List one after other at middle position
- Insert a node at a specific position in a linked list
- Delete a Doubly Linked List node at a given position
- Insertion at Specific Position in a Circular Doubly Linked List
- Create linked list from a given array
- Multiply two numbers represented as linked lists into a third list
- Divide the given linked list in two lists of size ratio p:q
- In-place Merge two linked lists without changing links of first list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.