Given a pointer that points to an intermediate node (non-head, non-tail) on a single-link list . How to insert a new node just before the node pointed to by the given pointer ?
Example, Given single-linked list:
A -> B -> C -> D -> E
Given a pointer pointed to C (ptr = &C), and a new node F, how to get
A -> B -> F -> C -> D -> E
Attention: we do not have pointers pointed to A.
Thanks
You should be able to implement this by inserting a new C node to the right, and writing F in the data field of the initial C node.
Obviously, It can't be done. If you need that capability, use a doubly-linked list. Otherwise, always pass along a pointer to the head of the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With