Thursday, April 25, 2019

Swap Nodes of Linked List without Copying


C++ Code:


void swap_nodes(Node **headPtr, int a, int b){
    if(a == b || !headPtr || !(*headPtr))
        return;
    
    Node *curr = *headPtr;
    Node *prev = nullptr;
    Node *nodeA = nullptr;
    Node *nodeB = nullptr;
    Node *prevA = nullptr;
    Node *prevB = nullptr;
    
    while (curr) {
        if(curr->data == a){
            nodeA = curr;
            prevA = prev;
        } else if(curr->data == b){
            nodeB = curr;
            prevB = prev;
        }
        prev = curr;
        curr = curr->next;
    }
    
    if(!nodeA || !nodeB)
        return;
    
    if(prevA){
        prevA->next = nodeB;
    } else { // nodeA is head
        *headPtr = nodeB;
    }
    
    if(prevB){
        prevB->next = nodeA;
    } else { // nodeB is head
        *headPtr = nodeA;
    }
    
    Node *tmp = nodeA->next;
    nodeA->next = nodeB->next;
    nodeB->next = tmp;
}


Detailed explanation is available on youtube:  


Subscribe toKnowledgeCenter 


Monday, April 22, 2019

Remove Loop From Linked List using Floyd's Algorithm



C++ Code:


void remove_loop(Node *head){
    Node *slow = head;
    Node *fast = head;
    if (!head || !(head->next))
        return;
    while(slow && fast && fast->next){
        slow = slow->next;
        fast = fast->next->next;
        if(slow == fast)
            break;
    }
    
    // No Loop exixts
    if (slow != fast){
        std::cout << "No Loop" << std::endl;
        return;
    }
    
    // When Loop exists
    slow = head;
    while(slow->next != fast->next){
        slow = slow->next;
        fast = fast->next;
    }
    fast->next = nullptr;
}



Detailed explanation is available on Youtube:

Subscribe to KnowledgeCenter

Friday, April 19, 2019

Find First Element of Loop in Linked List




C++ Code:

void find_loop_beginning(Node *head){
    Node *slow = head;
    Node *fast = head;
    if (!head || !(head->next))
        return;
    while(slow && fast && fast->next){
        slow = slow->next;
        fast = fast->next->next;
        if(slow == fast)
            break;
    }
    
    // No Loop exixts
    if (slow != fast){
        std::cout << "No Loop" << std::endl;
        return;
    }
    
    // When Loop exists
    slow = head;
    while(slow != fast){
        slow = slow->next;
        fast = fast->next;
    }
    std::cout << "First loop element = " << slow->data << std::endl;
}

Detailed explanation can be found on Youtube:


Subscribe to KnowledgeCenter

Find k-th node from end in Linked List



void print_kth_from_end(Node *head, int k){
    Node *lead = head;
    Node *lag = head;
    while(k > 0){
        if(lead){
            k--;
            lead = lead->next;
        }
        else{
            std::cout << "K is larger than length of Linked List" << std::endl;
            return;
        }
    }
    while(lead){
        lead = lead->next;
        lag = lag->next;
    }
    std::cout << "kth node from end = " << lag->data << std::endl;
}


Detailed explanation can be found on Youtube:


Subscribe to KnowledgeCenter

Thursday, April 18, 2019

Find the middle element of a Linked List


void print_middle(Node *head){
    Node *fast = head;
    Node *slow = head;
    while(fast && fast->next){
        fast = fast->next->next;
        slow = slow->next;
    }
    std::cout << slow->data << std::endl;
}

Watch the detailed explanation of the concepts on Youtube:

Subscribe to KnowledgeCenter

LeetCode 30 Day Challenge | Day 7 | Counting Elements

Given an integer array  arr , count element  x  such that  x + 1  is also in  arr . If there're duplicates in  arr , count them sepe...