Given a linked list with some two adjacent repeating nodes before a zero, task is to double the first and make next 0. After this, append all the zeros to tail.
Prerequisite: Basics of implementation of Singly Linked List
Input : 4 -> 4 -> 0 -> 2 -> 3 -> 4 -> 3 -> 3 -> 0 -> 4 -> Output : 8-> 2-> 3-> 4-> 6-> 4-> 0-> 0-> 0-> 0-> Explanation : First, after doubling the first element and making second element 0 before all zeros. 8 -> 0 -> 0 -> 2 -> 3 -> 4 -> 6 -> 0 -> 0 -> 4 -> Next : 8 -> 6 -> 5 -> 6 -> 0 -> 0 -> 0 -> 0 -> 0 -> 0 -> 0 -> Input : 0 -> 4 -> 4 -> 0 -> 3 -> 3 -> 0 -> 5 -> 0 -> 0 -> 6 -> Output : 8 -> 6 -> 5 -> 6 -> 0 -> 0 -> 0 -> 0 -> 0 -> 0 -> 0 ->
Traverse through the linked list, and wherever there are two adjacent same data of nodes before a 0 (e.g. 4 -> 4 -> 0), then, double first element and make another as 0 (e.g. 8 -> 0 -> 0 ->). Finally, traverse the linked list and linearly point all the zeros to tail.
Original linked list : 4 -> 4 -> 0 -> 2 -> 3 -> 4 -> 3 -> 3 -> 0 -> 4 -> Modified linked list : 8 -> 2 -> 3 -> 4 -> 6 -> 4 -> 0 -> 0 -> 0 -> 0 ->
Time complexity : O(n), where n is the number of nodes of linked list.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Given a linked list, reverse alternate nodes and append at the end
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Append the last M nodes to the beginning of the given linked list
- Move all zeros to the front of the linked list
- Delete multiple occurrences of key in Linked list using double pointer
- Difference between Singly linked list and Doubly linked list
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Merge a linked list into another linked list at alternate positions
- Check if a linked list is Circular Linked List
- Convert singly linked list into circular linked list
- Convert Singly Linked List to XOR Linked List
- Create new linked list from two given linked list with greater element at each node
- Partitioning a linked list around a given value and If we don't care about making the elements of the list "stable"
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
- Construct a Doubly linked linked list from 2D Matrix
- Find smallest and largest elements in singly linked list
- Find minimum and maximum elements in singly Circular Linked List
- Sublist Search (Search a linked list in another 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.