I have this problem where you have a list of odd or even number of items in doubly link list and you have delete the middle one. For the even number one you delete the two middle elements, say you have 1,2,3,...,10 you delete 5 and 6, and for the odd one say for 1,2,3,4,5 you delete 3.

The problem is that you do not know whether the list is even or odd and you have to delete the middle element(s). I have to write a pseudo-code procedure to delete the middle (one or two) record(s) of a doubly linked list without counting first or knowing the total number of elements in the list. So what approach can I use to do this?

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Sky Diploma

You could try using 2 pointers, where you increment the first pointer by 1 and the second pointer by 2. That way when the second pointer reaches the end of the list, the first one is at the middle. You can figure out whether its odd or even by making a note of how much you have to increment the second pointer by just before it reaches the end of the list.

Or,

you can use 2 pointers pointing to the first and last element of the list and then increment 1 and decrement the other. Until we get to the centre of the list . then delete that particular node(s)

This way we dont count or, know how many elments we have.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.