Google | Facebook | Phone Screen | 3 Solutions | Insert Node Into Sorted Circular List

942
Oct 25, 2023
Jan 05, 2025

😊 ~ 𝙒𝙞𝙩𝙝 ❤️ 𝙗𝙮 𝙃𝙞𝙧𝙚𝙣

Note: Hey There! Just Take A Look At The Code And You'll Get It.
Note: Time Complexity And Auxiliary Space Is Mentioned With The Code.

Approach 1 : Using Sorting + Buffer

class InsertUsingVector {
    static bool comp(Node* node1, Node* node2) {
        return node1->data < node2->data;
    }
    
public:
    // Method to insert a new node in the circular list, using sorting with vector - O(NLogN) & O(N)
    Node* sortedInsertToList(Node* headNode, int key) {
        if(!headNode)
            return new Node(key);
            
        vector<Node*> nodes;
        nodes.push_back(new Node(key));
        Node* currNode = headNode;
        
        int sz = 1, seeingFirstNodeFirstTime = 1;
        
        while(seeingFirstNodeFirstTime || currNode != headNode) {
            nodes.push_back(currNode);
            currNode = currNode->next;
            seeingFirstNodeFirstTime = 0;
            sz++;
        }
        
        sort(begin(nodes), end(nodes), comp);
        
        for(int index = 1; index < sz; ++index) {
            nodes[index - 1]->next = nodes[index];
        }
        nodes[sz - 1]->next = nodes[0];
        
        return nodes[0];
    }
};

Approach 2 : Using Dynamic Node Creation

class InsertByCreatingNewList {
public:
    // Method to insert a new node in the circular list, using dynamic node creation - O(N) & O(N)
    Node* sortedInsertToList(Node* headNode, int key) 
        if(!headNode)
            return new Node(key);
        
        Node* dummyHead = new Node(-1);
        Node* dummyTail = dummyHead;
        Node* currNode  = headNode;
        
        bool seeingFirstNodeFirstTime = true, isNodeInserted = false;
        
        while(seeingFirstNodeFirstTime || currNode != headNode) {
            if(!isNodeInserted && currNode->data > key) {
                dummyTail->next = new Node(key);
                dummyTail = dummyTail->next;
                isNodeInserted = true;
            }
            dummyTail->next = new Node(currNode->data);
            dummyTail = dummyTail->next;
            currNode  = currNode->next;
            seeingFirstNodeFirstTime = false;
        }
        
        if(!isNodeInserted) {
            dummyTail->next = new Node(key);
            dummyTail = dummyTail->next;
        }
            
        dummyTail->next = dummyHead->next;

        return dummyHead->next;
    
};
// Note: This solution contains memory leak problem as we're creating the new nodes dynamically but never freed

Approach 3 : Using Constant Auxiliary Space

class InsertWithoutCreatingList {
public:
    // Method to insert a new node in the circular list, using constant auxiliary space - O(N) & O(1)
    Node* sortedInsertToList(Node* headNode, int key) {
        if(!headNode)
            return new Node(key);
        
        Node* dummyHead = new Node(-1);
        Node* dummyTail = dummyHead;
        Node* headNode  = headNode;
        Node* nextNode  = nullptr;
        
        bool seeingFirstNodeFirstTime = true, isNodeInserted = false;
        
        while(seeingFirstNodeFirstTime || headNode != headNode) {
            nextNode = headNode->next;
            
            if(!isNodeInserted && headNode->data > key) {
                dummyTail->next = new Node(key);
                dummyTail = dummyTail->next;
                isNodeInserted = true;
            }
            
            dummyTail->next = headNode;
            dummyTail = dummyTail->next;
            headNode  = nextNode;
            seeingFirstNodeFirstTime = false;
        }
        
        if(!isNodeInserted) {
            dummyTail->next = new Node(key);
            dummyTail = dummyTail->next;
        }
            
        dummyTail->next = dummyHead->next;
        headNode = dummyHead->next;
        delete dummyHead;
        
        return headNode;
    }
};

𝗨𝗣𝗩𝗢𝗧𝗘 𝗜𝗙 𝗬𝗢𝗨 𝗟𝗜𝗞𝗘 𝗧𝗛𝗘 𝗦𝗢𝗟𝗨𝗧𝗜𝗢𝗡 👍

Note: This solutions are created by myself after reading this posts :-
Link1 - https://leetcode.com/discuss/interview-question/435345/Google-or-Phone-or-Insert-into-a-Sorted-Circular-Linked-List
Link2 - https://leetcode.com/discuss/interview-question/429160/Facebook-or-Phone-Screen-or-Insert-into-a-Sorted-Circular-Linked-list

Comments (5)