Given a linked list of alphabets and special characters. Reverse the given linked list without affecting the position of the special characters.
Input: g -> @ -> e -> # -> e -> $ -> k -> s -> NULL
Output: s -> @ -> k -> # -> e -> $ -> e -> g -> NULL
Explanation: Here we can see that in the output the position of special character in not change and also linked list is reverse.
The idea is to traverse the linked list and store the characters excluding the special characters in a temporary array. Again traverse the linked list and copy elements from the array to the nodes of the linked list in a reverse manner.
Below is the step by step algorithm:
- Take a temporary array, TEMP_ARR.
- Traverse the linked list and do the following
- if the current element is an alphabet, store that element of the linked list to TEMP_ARR.
- else, increase node pointer by one
Again traverse the linked list from the head and TEMP_ARR from the end and do the following:
- if the current element is an alphabet, copy the last element of TEMP_ARR to the current linked list node and decrease the current index of TEMP_ARR for the next iteration.
- else, increase node by one
Below is the implementation of above approach:
Given linked list: geek$sforg#@eek$s Reversed Linked list: skee$grofs#@kee$g
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.
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Remove all special characters from a singly Linked List
- Print reverse of a Linked List without extra space and modifications
- Print reverse of a Linked List without actually reversing
- Reverse alternate K nodes in a Singly Linked List
- Given a linked list, reverse alternate nodes and append at the end
- Print the last k nodes of the linked list in reverse order | Recursive approach
- Print the last k nodes of the linked list in reverse order | Iterative Approaches
- Reverse alternate K nodes in a Singly Linked List - Iterative Solution
- Swap nodes in a linked list without swapping data
- Delete N nodes after M nodes of a linked list
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Number of special nodes in an n-ary tree
- 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
- Difference between Singly linked list and Doubly linked 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 email@example.com. 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.