Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Implementing Linked List Operations: Reversal, Pairwise Swapping, and Nth Node Removal

Notes 1

LeetCode Problem 206: Reverse Linked List

Problem Description:

Reverse Linked List Diagram

Solution: Using a two-pointer approach with previous and current pointers, iteratively modify pointer directions. Pay atttention to the loop termination condition; return the previous pointer as the new head node.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int value;
 *     ListNode *nextNode;
 *     ListNode() : value(0), nextNode(nullptr) {}
 *     ListNode(int val) : value(val), nextNode(nullptr) {}
 *     ListNode(int val, ListNode *next) : value(val), nextNode(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* previous = nullptr;
        ListNode* current = head;
        while (current != nullptr) {
            ListNode* nextNode = current->nextNode;
            current->nextNode = previous;
            previous = current;
            current = nextNode;
        }
        return previous;
    }
};

LeetCode Problem 24: Swap Nodes in Pairs

Problem Description:

Swap Nodes in Pairs Diagram

Approach: Use a dummy head node and diagram the operation sequence.

Operation Sequence Diagram

Solution:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int value;
 *     ListNode *nextNode;
 *     ListNode() : value(0), nextNode(nullptr) {}
 *     ListNode(int val) : value(val), nextNode(nullptr) {}
 *     ListNode(int val, ListNode *next) : value(val), nextNode(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* dummyHead = new ListNode();
        dummyHead->nextNode = head;
        ListNode* current = dummyHead;
        while (current->nextNode != nullptr && current->nextNode->nextNode != nullptr) {
            // Skip if only one node remains
            ListNode* firstNode = current->nextNode;
            ListNode* secondNode = current->nextNode->nextNode;
            ListNode* thirdNode = current->nextNode->nextNode->nextNode;

            current->nextNode = secondNode; // Step 1
            secondNode->nextNode = firstNode; // Step 2
            firstNode->nextNode = thirdNode; // Step 3

            current = current->nextNode->nextNode;
        }
        ListNode* result = dummyHead->nextNode;
        delete dummyHead;
        return result;
    }
};

LeetCode Problem 19: Remove Nth Node From End of List

Problem Description:

Remove Nth Node Diagram

Solutino: Two-pointer technique with fast and slow pointers. The slow pointer lags behind the fast pointer by n+1 steps. When the fast pointer reaches nullptr, the slow pointer points to the node preceding the one to be removed.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int value;
 *     ListNode *nextNode;
 *     ListNode() : value(0), nextNode(nullptr) {}
 *     ListNode(int val) : value(val), nextNode(nullptr) {}
 *     ListNode(int val, ListNode *next) : value(val), nextNode(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummyHead = new ListNode();
        dummyHead->nextNode = head;
        ListNode* slow = dummyHead;
        ListNode* fast = dummyHead;
        while (n-- && fast->nextNode != nullptr) {
            fast = fast->nextNode;
        }
        fast = fast->nextNode;
        while (fast != nullptr) {
            slow = slow->nextNode;
            fast = fast->nextNode;
        }
        ListNode* nodeToDelete = slow->nextNode;
        slow->nextNode = slow->nextNode->nextNode;
        delete nodeToDelete;
        ListNode* result = dummyHead->nextNode;
        delete dummyHead;
        return result;
    }
};

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

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