Velocity Reviews > C++ > Delete every mth element in a linked list ??

# Delete every mth element in a linked list ??

Guest
Posts: n/a

 12-20-2006
This is a normal interview question, which goes like this
"Given a linked list of integers, delete every mth node and return the
last remaining node."

Delete every 3rd node (m = 3) and return the last remaining node
meaning...
4->6->3->5->10->1->17
4->3->5->1->17
3->5->17
3->17
3

after deleting every 3rd node the last remaining node is 3, so node
with data 3 is returned.
function prototype:

{
}

thanks
A

Alf P. Steinbach
Guest
Posts: n/a

 12-20-2006
> This is a normal interview question, which goes like this
> "Given a linked list of integers, delete every mth node and return the
> last remaining node."
>
> eg: Linked list = 4->6->7->3->5->6->10->1->23->17
> Delete every 3rd node (m = 3) and return the last remaining node
> meaning...
> 4->6->3->5->10->1->17
> 4->3->5->1->17
> 3->5->17
> 3->17
> 3
>
> after deleting every 3rd node the last remaining node is 3, so node
> with data 3 is returned.
> function prototype:
>
> node* deleteEveryMth(node** head, int m)
> {
> }

Although the article quoted above is off-topic, it's interesting that
there's seemingly no consistent system in the example, and an
underspecified and at the same time overspecified example to be filled
out. With a meaningless argument. Are you sure this was all part of
the homework problem, or is it your elaboration?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a

 12-20-2006
On Dec 20, 10:33 am, "Aditya" <(E-Mail Removed)> wrote:
> This is a normal interview question, which goes like this
> "Given a linked list of integers, delete every mth node and return the
> last remaining node."
>
> eg: Linked list = 4->6->7->3->5->6->10->1->23->17
> Delete every 3rd node (m = 3) and return the last remaining node
> meaning...
> 4->6->3->5->10->1->17
> 4->3->5->1->17
> 3->5->17
> 3->17
> 3
>
> after deleting every 3rd node the last remaining node is 3, so node
> with data 3 is returned.
> function prototype:
>
> node* deleteEveryMth(node** head, int m)
> {
>
> }

Start by rewriting that as:

template<class T>
T& deleteEveryMth(std::list<T>, unsigned int m)
{
}

--
Erik Wikström

Rud1ger Sch1erz
Guest
Posts: n/a

 12-20-2006

> after deleting every 3rd node the last remaining node is 3, so node
> with data 3 is returned.

Well, could be solved with some kind of recursive function call.

Cheers,
Rudiger

--
The next sentence is wrong.
The previous sentence is right.

Salt_Peter
Guest
Posts: n/a

 12-20-2006

> This is a normal interview question, which goes like this
> "Given a linked list of integers, delete every mth node and return the
> last remaining node."
>
> eg: Linked list = 4->6->7->3->5->6->10->1->23->17
> Delete every 3rd node (m = 3) and return the last remaining node
> meaning...
> 4->6->3->5->10->1->17
> 4->3->5->1->17
> 3->5->17
> 3->17
> 3
>
> after deleting every 3rd node the last remaining node is 3, so node
> with data 3 is returned.
> function prototype:
>
> node* deleteEveryMth(node** head, int m)
> {

Looks like C, not C++.
A list of nodes should never expose its nodes. deleteEveryMth(...)
needs to be templated and takes a reference to a List, returns the
template parameter ( see Erik's Post ).

> }
>
> thanks
> A

Mark P
Guest
Posts: n/a

 12-20-2006
> This is a normal interview question, which goes like this
> "Given a linked list of integers, delete every mth node and return the
> last remaining node."
>
> eg: Linked list = 4->6->7->3->5->6->10->1->23->17
> Delete every 3rd node (m = 3) and return the last remaining node
> meaning...
> 4->6->3->5->10->1->17
> 4->3->5->1->17
> 3->5->17
> 3->17
> 3
>
> after deleting every 3rd node the last remaining node is 3, so node
> with data 3 is returned.
> function prototype:
>
> node* deleteEveryMth(node** head, int m)
> {
> }
>
> thanks
> A
>

Your description makes no sense as written (how do you know there's only
one remaining node?) nor does it agree with your example ('6' is the
second node in the list so how does it get deleted in step two?). I
is how to delete every mth node from a _circularly_ linked list until
only one node remains.

Why don't you give it a try and show us what you come up with?