Remove a node at the tail of a linked list C

delete last node in linked list in c
delete a node from linked list in c
delete last node in linked list c++
delete node at given position in a linked list
delete a node from linked list algorithm
delete first node in linked list in c
delete last node in linked list python
delete node linked list c++

I wrote code to remove the node at the tail of a linked list. The code works properly in different testcases, but I think I made my code a bit cumbersome. However, I don't see what I can do differently?

node_t *remove_t (node_t *l){
if (l==NULL){
    return l;
}
else {
    node_t *curr=l;
    node_t *ret=l;
    if (curr->next==NULL){
        l=NULL;
        return l;
    }
    else {
        while (curr->next->next!=NULL){
            curr=curr->next;
        }
        curr->next=NULL;
        free(curr->next);
        return ret;
    }
}
}

I'm not sure that you can change the logic much - as your approach of 3 different cases (empty list, list with 1 item, and list with >1 items) is reasonable. You can format the code for easier reading: Something like:

node_t *remove_t (node_t *l){
    // case 1: Empty list
    if (l==NULL){
        return l;
    } ;

    // case 2: List with one item. Return empty list.
    node_t *curr=l;
    if (curr->next==NULL){
        // Remember to free this element.
        free(curr) ;
        l=NULL;
        return l;
    } ;

    // case 3: list > one item
    // Move curr to last item
    while (curr->next->next!=NULL){
        curr=curr->next;
    }
    // Note order of free/null setting.
    free(curr->next);
    curr->next=NULL;

    return l;
}

Given only a pointer to a node to be deleted in a singly linked list, how, How do you remove a node from a linked list? We delete any node of a linked list by connecting the predecessor node of the node to be deleted by the successor node of the same node. For example, if we have a linked list a → b → c, then to delete the node ‘b’, we will connect ‘a’ to ‘c’ i.e., a → c.

It is much, much easier if you keep a pointer-to-pointer to node, then iterate to the end of list and simply free the last pointer and set it NULL, e.g.

/** delete last node */
void del_last_node (node_t **l)
{
    node_t **ppn = l;       /* pointer to pointer */
    node_t *pn = *l;        /* pointer to node */

    for (; pn->next; ppn = &pn->next, pn = pn->next) { } /* iterate to last */

    free (*ppn);           /* free node */
    *ppn = NULL;           /* set pointer NULL */
}

Linked List, How do you remove a node from a linked list in Java? How to Delete Node in a Linked List (Given Only Access to That Node)? The advantage of Linked List (using pointers) is that it is constant time to insert and delete a node from the list. If the root node is given, you can search the node from the root using O(n) linear search to find the previous node of the to-be-deleted node.

You didn't seem to free the tail node.

curr->next=NULL;
free(curr->next);

You won't be able to free curr->next if you already make it NULL.

My implementation:

void remove_tail(node_t *l) {
    if (l == NULL) return;
    if (l->next == NULL) {
        free(l);
        l = NULL;
        return;
    }
    node_t *prev = l;
    node_t *curr = l->next;
    while (curr->next != NULL) {
        prev = curr;
        curr = curr->next;
    }
    prev->next = NULL;
    free(curr);
}

Remove node at the TAIL Linked List C++, A complete working C program to demonstrate deletion in singly. // linked list. #​include <stdio Duration: 4:35 Posted: Jun 8, 2016 We delete any node of a linked list by connecting the predecessor node of the node to be deleted by the successor node of the same node. For example, if we have a linked list a → b → c, then to delete the node ‘b’, we will connect ‘a’ to ‘c’ i.e., a → c. But this will make the node ‘b’ inaccessible and this type of inaccessible nodes are called garbage and we need to clean this garbage. We do this cleaning by the use of ‘delete’ operator.

Deleting a node at the tail of a list, There are many ways this can be done, following is one: int List::removeAtTail(){ while(curr != NULL) { if(curr->next == NULL) { // depending on your  Algorithm: If the first node is null or there is only one node, then return null. if headNode == null then return null if headNode .nextNode == null then free head and return null. Create an extra space secondLast, and traverse the linked list till the second last node. while secondLast .nextNode.nextNode != null secondLast = secondLast .nextNode.

SINGLY-LINKED LIST :: REMOVAL (DELETION) ALGORITHM (Java , You cannot go backward in a (simple) linked list. We can find the last node in a list as follows: ptr = head  Remove all even parity nodes from a Doubly and Circular Singly Linked List Design a structure which supports insertion and first non-repeating element in O(1) time Sum and Product of all even digit sum Nodes of a Singly Linked List

C exercises: Delete the last node of Singly Linked List, It can be done in three steps: Update tail link to point to the node, before the tail. In order to find it, list should be traversed first, beginning from the head. Set next link of the new tail to NULL. Dispose removed node. The usual way of deleting a node node from a linked list is to modify the next pointer of the node before it, to point to the node after it. Since we do not have access to the node before the one we want to delete, we cannot modify the next pointer of that node in any way.

Comments
  • This sounds like a better question for Code Review.
  • curr->next=NULL; overwrites the pointer at curr->next (creating a memory leak), then calling free(curr->next); has no effect. Equivalent to free (NULL);. Always use valgrind (or a similar memory use/error checking program.
  • Nice solution, need to handle the empty list case (*l == null).
  • Yes that can be added with if (!*l) return;, but I generally ensure a list is not NULL before calling functions that operate on it. But to protect the user from themselves completely, it isn't a bad idea.