![]() |
VOOZH | about |
We need to implement a doubly linked list with the use of a single pointer in each node. For that we are given a stream of data of size n for the linked list, your task is to make the function insert() and getList(). The insert() function pushes (or inserts at the beginning) the given data in the linked list and the getList() function returns the linked list as a list.
Note: The List should be printed in both forward and backward direction.
Examples
Input: head= 40<->30<->20<->10
Output: 40 30 20 10
10 20 30 40Input:head= 5<->4<->3<->2<->1
Output: 5 4 3 2 1
1 2 3 4 5
We know that each node in a doubly-linked list has two pointer fields which contain the addresses of the previous and next node. On the other hand, each node of the XOR linked list requires only a single pointer field, which doesn’t store the actual memory addresses but stores the bitwise XOR of addresses for its previous and next node.
Below is the implementation of the above approach :
40 30 20 10 10 20 30 40
Time Complexity: O(n) for both insertion and retrieval, where n is the number of nodes.
Auxiliary Space: O(1) for insertion, O(n) for storing the list in an array
Related articles :