View Single Post
  #9 (permalink)  
Old August 23rd, 2007, 09:02 AM
mkatiyar mkatiyar is offline
Registered User
 
Join Date: Aug 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Answer to linked list question.

1) have two pointers, lets say we want to delete 5th node from last (of course assuming that the list has so many nodes)

 move the first one by 5 and keep second at head.
 Now start moving both of them by 1
 When your second reaches end, your first one is pointing 5th node from last. Delete it !!! :-)

2)If your array is such that it has 2n+1 elements where n elements are duplicated, just do a xor of all the elements like
  for(i=0;i<2n+1;i++)
      result^=arr[i];
  at the end of the loop you will have the non-duplicate number in the result. Now compare each element and remove it.

If you have multiple non-duplicates, you need to sort and then compare adjacent elements to remove.

Reply With Quote