views:

349

answers:

2

Given a linked list of T size , select first 2n nodes and delete first n nodes from them; Then do it for the next 2n nodes and so on...

For example- Let's consider a linked list of size 7:

  `1->2->3->4->5->6->7`

If n = 2, the desired output is :

  `1->2->5->6->7`

I didn't understand what this problem is actually indicating.Could somebody help me to understand the problem ?

EDIT : Adding C and C++ tags so that this may reach to more eye balls, and of-course those are only two languages allowed in the interview itself.

+2  A: 

That actually looks like it should say:

Given a linked list of T size , select first 2n nodes and delete last n nodes from them; Then do it for the next 2n nodes and so on...

or:

Given a linked list of T size , select first 2n nodes and keep first n nodes from them; Then do it for the next 2n nodes and so on...

That would mean select 1,2,3,4 then delete 3,4 (or keep 1,2 which is the same thing). Then select 5,6,7,8, not possible so stop.

paxdiablo
Yes it seems there is a flaw in the statement.The last should be first :)
nthrgeek
+1 and accepted :)
nthrgeek
+1  A: 

Hi

I think it's even simpler than @paxdiablo indicates ...

do
  take n
  skip n
until you run out of elements to take or skip
High Performance Mark