Skip to main content

Linked List Delete

Source: Asked to me by Ankush Jain (CSE IITB 2011 Alumnus, Morgan Stanley Quant)

Problem:
You are given a pointer to a node (not the tail node) in a singly linked list. Delete that node from the linked list. Write code in C.

Update (December 13, 2011):
Solution posted by Siddhartha in comments! Interesting comment by me in comments!

Comments

  1. Probably one of the most popular CS Interview questions.

    boolean delete(Node* node){
    if(node->next==NULL) return false;
    else{
    node->val=node->next->val;
    node->next=node->next->next;
    return true;
    }
    }

    ReplyDelete
  2. If playing with the data part of the node is allowed, copying the next node into the current node and then deleting the next node will suffice.

    ReplyDelete
  3. Another interesting question on Linked List,which I was asked recently for one of my Job interviews : You are given a linked list(the head pointer). One of the nodes in the linked list points to an earlier node in the list(a loop). Find that 'earlier' node to which this node points to. Time Complexity : O(n)
    Space Complexity O(1).

    ReplyDelete
  4. In the C code for the linked list, are you allowed access to the data?
    If so, copy the data for the next node to this node, and then delete the next node. (by setting the "next" pointer of this node (say n) to n->next->next)

    ReplyDelete
  5. If O(n) can't be done, then the only way I can think of is to move next node data to the current node and delete the next node. But you need to assume that there aren't other pointers to these nodes - else you are making some valid pointers (valid in the O(n) soln) invalid and vice versa which may not really be the right thing to do in the context of the program.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Let the pointer to the node be point.Then the code is as follows:

    current=point->next->next;point->info=point->next->info;
    point->next=current;

    ReplyDelete
  8. Ankush has already given the correct solution.
    However, just for technical details sake, we need to free the next node too using free(node->next)
    So another possible solution might be(copying the rest of the code from ankush)

    boolean delete(Node* node){
    if(node->next==NULL) return false;
    else{
    node->val=node->next->val;
    struct node *temp=node->next;
    node->next=node->next->next;
    free(temp);
    return true;
    }
    }

    ReplyDelete
  9. All comments point to the correct solution, but Siddhartha's solution is most apt. Thanks.

    One point I would like to make:
    If the pointer is the last element of the linked list, then is there no way to delete the last element in O(1).. or is there? In Siddhartha's code, "if(node->next==NULL) return false; " does not delete the node. Thanks

    ReplyDelete
  10. Verified my point with smarter people - All agree with me. So, we have O(1) solution provided the node is not the tail. If the node is a tail, we have O(n) algorithm.

    Thanks

    ReplyDelete

Post a Comment

Popular posts from this blog

Asking a girl out

This is not a puzzle. So, for those of you who follow this puzzle blog, please bear with me for just one post. Interesting Math in this article though :P

Most of my friends already read an article that I wrote more than an year back - "Speak Up"


Here, inspired by the movie, The Beautiful Mind, I give a mathematical analysis of asking a girl out. Nice time it is. Feb 10. No plans for Feb 14 and I am sure this article makes me look even more geekier and all the more reason for me to believe that I will be alone, yet again. But what the hell, lets do it!

Note: This is not an independent analysis. There are many "mathematics sites" which does "similar" analysis.

@Consultants, correct me if I am wrong in my estimates. :P

Why is there a need to be selective?

From the age of 15, I guess there are approximately 3,600 girls I have liked (On average days, I don't see new girls. But going outside, I like about 30 girls. Saying that I go out once every week right …

Consecutive Heads

Let's say A keep tossing a fair coin, until he get 2 consecutive heads, define X to be the number of tosses for this process; B keep tossing another fair coin, until he get 3 consecutive heads, define Y to be the number of the tosses for this process.

1) Calculate P{X>Y}
2) What's the expected value of X
3) What's the expected value of Y

This is probably the hardest puzzle I have ever put on my blog. Hence, I will post its solution in the post directly rather than on comment.

Solution:
1)
(Solved by me finally after 13 months :))

Make a state diagram. Let the state be (a,b) where a is the number of consecutive heads streak "A" is on currently and b is the number of consecutive heads streak "B" is on currently.

So, (0,3) (1,3) are final accepted states and (2,0) (2,1) (2,2) (2,3) are failure states. If you get tails, your contribution to the state reaches to "0"

f(State) = P(X>Y | "State" configuration initially)

f(0,0) = 1/4[f(…

Fraction Brainteaser

Source:
Sent to me by Gaurav Sinha

Problem:
Siddhant writes a Maths test and correctly answers 5 out of 6 Arithmetic questions and 20 out of 28 Geometry questions. In total, Siddhant scores 25 out of 34. 

Vaibhav writes another Maths test and correctly answers 20 out of 25 Arithmetic questions and 6 out of 9 Geometry questions. in total, Vaibhav scores 26 out of 34.

Note that
a) Vaibhav scores more than Siddhant
b) Siddhant score better than Vaibhav in both individual topics - 5/6 > 20/25 and 20/28 > 6/9

How is it possible?