Delete node from singly linked list when header is not known

Discussion in 'C++' started by Raj, Jan 30, 2006.

  1. Raj

    Raj Guest

    Is there any way to delete a particular node from a singly linked list
    where the header of the list is unknown.Only pointer available is the
    one which points to the node to be deleted
     
    Raj, Jan 30, 2006
    #1
    1. Advertising

  2. Raj

    Tosha Guest

    "Raj" <> wrote in message
    news:...
    > Is there any way to delete a particular node from a singly linked list
    > where the header of the list is unknown.Only pointer available is the
    > one which points to the node to be deleted
    >


    type *temp = ptr;
    ptr = ptr->next;
    free(temp);
     
    Tosha, Jan 30, 2006
    #2
    1. Advertising

  3. Raj

    Raj Guest

    But I want to retain the list. I would be losing the link with the
    previous node if I do wats mentioned
     
    Raj, Jan 30, 2006
    #3
  4. Raj

    Tosha Guest

    "Raj" <> wrote in message
    news:...
    > But I want to retain the list. I would be losing the link with the
    > previous node if I do wats mentioned
    >

    No you would not, because the the pointer you have has the adress of the
    node you want to delete and value of previous_node->next at the same time
    (that's is the same thing), and when you say "ptr = ptr->next" you change
    the value of previous_node->next to point to previous_node->next->next or
    ptr->next.
    I don't know if you have double indirection connection with list or single,
    because if you have single, than it's only possible to delete node in front
    of pointed node, like "ptr->next = ptr->next->next".
     
    Tosha, Jan 30, 2006
    #4
  5. Raj

    Jaspreet Guest

    Raj wrote:
    > Is there any way to delete a particular node from a singly linked list
    > where the header of the list is unknown.Only pointer available is the
    > one which points to the node to be deleted


    This one's a popular interview question. Just copy the value of the
    next node to the current node and delete the next node. So, it would
    be something like :

    node *tmp = NULL;
    tmp = ptr->next;
    ptr->value = tmp->value;
    ptr->next=tmp->next;
    delete tmp;
     
    Jaspreet, Jan 30, 2006
    #5
  6. Raj

    Howard Guest

    "Tosha" <> wrote in message
    news:drkp4k$iub$-com.hr...
    >
    > "Raj" <> wrote in message
    > news:...
    >> But I want to retain the list. I would be losing the link with the
    >> previous node if I do wats mentioned
    >>

    > No you would not, because the the pointer you have has the adress of the
    > node you want to delete and value of previous_node->next at the same time
    > (that's is the same thing), and when you say "ptr = ptr->next" you change
    > the value of previous_node->next to point to previous_node->next->next or
    > ptr->next.


    What? When you change the pointer variable ptr, which currently points to
    the node to be deleted, in what way does that change the value of the
    previous node's "next" member? Just because the current values of those
    pointers are the same, does not mean they're the same pointers! After
    assigning a new value to ptr, ptr now points to the next node, but the
    previous node's next pointer is unchanged! After deleting the current node,
    it becomes an invalid pointer.

    The correct method is given by Jaspreet (although the first two lines of
    that example might as well be combined into one.)

    -Howard

    > I don't know if you have double indirection connection with list or
    > single, because if you have single, than it's only possible to delete node
    > in front of pointed node, like "ptr->next = ptr->next->next".
    >


    I don't know what "double indirection" means in this context, but the
    requirement was for a "singly linked list". To me, "double indirection"
    means a pointer-to-a-pointer, as in node**.

    -Howard
     
    Howard, Jan 30, 2006
    #6
  7. Raj

    Tosha Guest

    "Howard" <> wrote in message
    news:UcqDf.571910$...
    >
    > "Tosha" <> wrote in message
    > news:drkp4k$iub$-com.hr...
    >>
    >> "Raj" <> wrote in message
    >> news:...
    >>> But I want to retain the list. I would be losing the link with the
    >>> previous node if I do wats mentioned
    >>>

    >> No you would not, because the the pointer you have has the adress of the
    >> node you want to delete and value of previous_node->next at the same time
    >> (that's is the same thing), and when you say "ptr = ptr->next" you change
    >> the value of previous_node->next to point to previous_node->next->next or
    >> ptr->next.

    >
    > What? When you change the pointer variable ptr, which currently points to
    > the node to be deleted, in what way does that change the value of the
    > previous node's "next" member? Just because the current values of those
    > pointers are the same, does not mean they're the same pointers! After
    > assigning a new value to ptr, ptr now points to the next node, but the
    > previous node's next pointer is unchanged! After deleting the current
    > node, it becomes an invalid pointer.


    I meant if you have adress of previous->next or to say adress of original
    ptr value holder.
     
    Tosha, Jan 30, 2006
    #7
  8. Raj

    Howard Guest

    "Tosha" <> wrote in message
    news:drli67$o6k$-com.hr...
    > "Howard" <> wrote in message
    > news:UcqDf.571910$...
    >>
    >> "Tosha" <> wrote in message
    >> news:drkp4k$iub$-com.hr...
    >>>
    >>> "Raj" <> wrote in message
    >>> news:...
    >>>> But I want to retain the list. I would be losing the link with the
    >>>> previous node if I do wats mentioned
    >>>>
    >>> No you would not, because the the pointer you have has the adress of the
    >>> node you want to delete and value of previous_node->next at the same
    >>> time (that's is the same thing), and when you say "ptr = ptr->next" you
    >>> change the value of previous_node->next to point to
    >>> previous_node->next->next or ptr->next.

    >>
    >> What? When you change the pointer variable ptr, which currently points
    >> to the node to be deleted, in what way does that change the value of the
    >> previous node's "next" member? Just because the current values of those
    >> pointers are the same, does not mean they're the same pointers! After
    >> assigning a new value to ptr, ptr now points to the next node, but the
    >> previous node's next pointer is unchanged! After deleting the current
    >> node, it becomes an invalid pointer.

    >
    > I meant if you have adress of previous->next or to say adress of original
    > ptr value holder.
    >


    The original post said that the ONLY thing we had was a pointer to the node
    to be deleted. Nothing in the original post suggested that the address of
    the previous node's next pointer was known,or even if there IS a previous
    node. (The node to be deleted might be the head of the list, after all.)

    The only way your code would affect the previous node's next pointer would
    be if you were given the pointer to the current node as a [non-const]
    reference to the previous node's next pointer, or if you were operating
    directly on that pointer, neither of which is suggested by the original
    post.

    -Howard
     
    Howard, Jan 30, 2006
    #8
  9. Raj

    Howard Guest

    "Jaspreet" <> wrote in message
    news:...
    >
    > Raj wrote:
    >> Is there any way to delete a particular node from a singly linked list
    >> where the header of the list is unknown.Only pointer available is the
    >> one which points to the node to be deleted

    >
    > This one's a popular interview question. Just copy the value of the
    > next node to the current node and delete the next node. So, it would
    > be something like :
    >
    > node *tmp = NULL;
    > tmp = ptr->next;
    > ptr->value = tmp->value;
    > ptr->next=tmp->next;
    > delete tmp;
    >


    [re-posting, because previous try doesn't seem to have gone through...]

    Two things to note in your example:

    1) (minor point): Might as well combine the frist two lines. No point in
    assigning NULL to the pointer and then assigning a value to it. Just
    declare and initialize in one statement.
    2) (major point): Before using tmp to assign the next and value members, you
    need to test that tmp is not NULL. Dereferencing a NULL pointer constitutes
    "undefined behavior". And if the current node is at the end of the list
    (assuming it's not circular), then its "next" pointer would be NULL (and so
    tmp would also be NULL).

    -Howard
     
    Howard, Jan 30, 2006
    #9
  10. Raj

    Guest

    Jaspreet wrote:
    > Raj wrote:
    > > Is there any way to delete a particular node from a singly linked list
    > > where the header of the list is unknown.Only pointer available is the
    > > one which points to the node to be deleted

    >
    > This one's a popular interview question. Just copy the value of the
    > next node to the current node and delete the next node. So, it would
    > be something like :


    And if there's no next node? How do you delete the last node?

    > node *tmp = NULL;
    > tmp = ptr->next;
    > ptr->value = tmp->value;
    > ptr->next=tmp->next;
    > delete tmp;


    You're writing to tmp twice. First you set it to 0, then to ptr->next.
    The first statement is obviously useless.
    Worse, you don't check for ptr->next==0.

    HTH,
    Michiel Salters
     
    , Jan 31, 2006
    #10
  11. Raj

    Raj Guest

    Thanks all of u for the replies....
    The suggestion is working out....But as Howard said the logic does not
    work if the node to be deleted is the last one.
    Any solutions??
     
    Raj, Feb 1, 2006
    #11
  12. Raj

    Ben Pope Guest

    Raj wrote:
    > Thanks all of u for the replies....
    > The suggestion is working out....But as Howard said the logic does not
    > work if the node to be deleted is the last one.
    > Any solutions??


    Perhaps the tail must be a special case. Some kind of "null" node which
    is copyable and always points to null.

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Feb 1, 2006
    #12
  13. Raj

    Howard Guest

    "Raj" <> wrote in message
    news:...
    > Thanks all of u for the replies....
    > The suggestion is working out....But as Howard said the logic does not
    > work if the node to be deleted is the last one.
    > Any solutions??
    >


    Nope. If the node to be deleted is the last one, then you're stuck. You
    should not delete it, because then you may at some point access it illegally
    through the next pointer of the previous node. In this case, I'd report an
    error.

    Another problem would be if the node were the first node in the list. Then,
    deleting it would invalidate the actual pointer to the head of the list.
    (Which you say you don't have... but I'm assuming this is just for the sake
    of this "puzzle", since any "real" program would certainly have to retain
    the pointer to the head of the list!)

    There is _no_way_, using a singly-linked list, and not having a pointer to
    the head of that list, to "properly" delete the given node from that list.
    You _can_ do like the example, making sure to check for NULL, of course, but
    you'd need to throw an error in that case. And, you'd need to tell whoever
    was supposedly using this method (your teacher? job interviewer?) that if
    this _were_ by chance the head node, then any existing pointers to the head
    node would no longer be valid.

    By the way, when given questions in interviews (if that's the kind of
    question this is), it's not always required that you come up with a perfect
    solution. Sometimes, what the interviewer wants is to hear your analysis of
    the problem. These kind of problems with the proposed solution are exactly
    the kind of things you need to be able to consider for yourself when
    designing real-world solutions. The more you're able to demonstrate your
    analytical abilities, the more likely you'll get the job.

    Of course, if this is just a puzzle posited by a friend or something, then
    the answer is that it's simply not possible to do correctly in all cases.
    Some, but not all.

    -Howard
     
    Howard, Feb 1, 2006
    #13
  14. Raj

    Raj Guest

    Ok..fine
    Deleting header wont be a probelm bcos if we knew it was header
    the qusetion itself would have been irrelevant

    Thanks a lot Howard...
     
    Raj, Feb 2, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick McCourt

    Stack & Singly Linked List Data Structures

    Patrick McCourt, May 24, 2004, in forum: Java
    Replies:
    2
    Views:
    964
    Kenneth P. Turvey
    May 24, 2004
  2. HS-MOON
    Replies:
    4
    Views:
    627
    Method Man
    Sep 24, 2004
  3. sangram
    Replies:
    16
    Views:
    2,081
  4. Replies:
    10
    Views:
    765
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Apr 16, 2007
  5. Replies:
    10
    Views:
    1,046
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Apr 16, 2007
Loading...

Share This Page