+ Reply to Thread
Results 1 to 2 of 2

Thread: Linked List Deletion

  1. #1
    TopGun Guest

    Linked List Deletion

    Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct linkedList{
        int element;
        struct linkedList* next;
    };
    
    typedef struct linkedList* List;
    
    void deleteNode(List Node)
    {
        List tmp;
        if(Node){ // If current node is not NULL
            tmp = List->next; // take backup of next Node
            Node->element = Node->next->element; // replace current node element with next Node element
            Node->next = Node->next->next; // change next pointer to next to next
            free(tmp); // free the next Node which was taken backup
        }
        return;
    }

  2. #2
    arif4123 is offline Junior Member
    Join Date
    Apr 2011
    Posts
    2
    This code doesn't handle the boundary cases like what if Node to be deleted is the last node i.e. Node->next=NULL , so in above code this condition should be handled as well.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Linked List merge
    By TopGun in forum Algorithm/Data Structure Questions
    Replies: 1
    Last Post: 30th May 2008, 15:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts