Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Efficient Algorithms for Linked List Operations

Tech 1

Removing Nodes with Specific Values from a Linked List

To delete all nodes with value x from a linked list L:

void remove_value_nodes(LinkList *L, ElemType target) {
    Node *current = L->next, *prev = L, *temp;
    while (current) {
        if (current->value == target) {
            temp = current;
            current = current->next;
            prev->next = current;
            free(temp);
        } else {
            prev = current;
            current = current->next;
        }
    }
}

Finding and Removing the Minimum Value Node

To locate and delete the node with the minimum value:

LinkList remove_min_node(LinkList *L) {
    Node *prev = L, *current = prev->next;
    Node *min_prev = prev, *min_node = current;
    
    while (current) {
        if (current->value < min_node->value) {
            min_node = current;
            min_prev = prev;
        }
        prev = current;
        current = current->next;
    }
    
    min_prev->next = min_node->next;
    free(min_node);
    return L;
}

Reversing a Linked List In-Place

Two approaches to reverse a linked list with O(1) space:

// Method 1: Iterative reversal
LinkList reverse_list_1(LinkList L) {
    Node *current = L->next, *next_node, *reversed = NULL;
    L->next = NULL;
    
    while (current) {
        next_node = current->next;
        current->next = reversed;
        reversed = current;
        current = next_node;
    }
    
    L->next = reversed;
    return L;
}

// Method 2: Pointer manipulation
LinkList reverse_list_2(LinkList L) {
    Node *prev = NULL, *current = L->next, *next_node;
    
    while (current) {
        next_node = current->next;
        current->next = prev;
        prev = current;
        current = next_node;
    }
    
    L->next = prev;
    return L;
}

Detecting Cycles in a Linked List

Using Floyd's cycle-finding algorithm:

Node *detect_cycle(Node *head) {
    Node *slow = head, *fast = head;
    
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
        
        if (slow == fast) {
            Node *start = head;
            while (start != slow) {
                start = start->next;
                slow = slow->next;
            }
            return start;
        }
    }
    
    return NULL;
}

Finding the Intersection Node of Two Linked Lists

Efficient approach to find common node:

Node *find_intersection(Node *headA, Node *headB) {
    int lenA = list_length(headA);
    int lenB = list_length(headB);
    
    Node *longer = lenA > lenB ? headA : headB;
    Node *shorter = lenA > lenB ? headB : headA;
    
    for (int i = 0; i < abs(lenA - lenB); i++)
        longer = longer->next;
    
    while (longer != shorter) {
        longer = longer->next;
        shorter = shorter->next;
    }
    
    return longer;
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.