The are just to warm you up..
First one is to find the middle element in the link list!
Hint: think about 2 pointers.
Second one is , delete a node P(given is the pointer to P) without traversing list to get to the previous node.
Hint: you shouldn't need one :)
Subscribe to:
Post Comments (Atom)
2 comments:
I hope by now people must have solved these two.
Here are my solutions.
1. Use two pointers P1 and P2. Traverse link list by increase P1 by 1 and P2 by 2 nodes in each iteration.
As soon as P2 becomes NULL then P1 points to middle element.
2. Key to this problem is that rather than changing link in the previous node, copy data to the next node and do so until you encounter same node P.
absolutely correct!
Post a Comment