I have to delete the previous node. I searched web but couldnt find the exact sol cuz the list given is singly list and we are not given any other pointer except the current one. How to do this? The list is neither circular nor doubly linked.
0
votes
What if there are two previous nodes? In a singly linked list, a node can have more than one previous.
– Raymond Chen
@Raymond Chen How two previous nodes?
– user3004790
Suppose you have three nodes. A.next = C. B.next = C. You are given C. What is its previous node?
– Raymond Chen
How can A and B both point to C in a singly linked list?
– user3004790
The list is in the way A->B->C, and u are given B and u have to delete A.
– user3004790
1 Answers
0
votes
I won't write the code, but you can get the gist of the algorithm. Note that this is assuming that this is a single-linked list structure
- Mark the current node of the list
- Iterate through the list with a loop. For each node,
- Check if the next node pointer is the marked node.
- If it is, execute the delete operation for a singly linked list.
- If it is not a marked node, move to the next node.
- Check if the next node pointer is the marked node.