Insert a node at a specific position in a linked list

Sort by

recency

|

1537 Discussions

|

  • + 0 comments

    Here is my C++ solution, you can watch the explanation here : https://youtu.be/jCPAp_UIzAs

    SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) {
        SinglyLinkedListNode* newNode = new SinglyLinkedListNode(data);
        if(position == 0) {
            newNode->next = llist;
            return newNode;
        }
        SinglyLinkedListNode* curr = llist;
        while(position - 1) {
            position--;
            curr = curr -> next;
        }
        newNode -> next = curr->next;
        curr->next = newNode;
        return llist;
    }
    
  • + 0 comments

    Problem has a confusing description. In the problem description tells you to return the list head node but the comments in the code tell you to return the whole list which can be confusing. INTEGER_SINGLY_LINKED_LIST should be INTEGER_SINGLY_LINKED_LIST_NODE (LIST HEAD)

     * The function is expected to return an INTEGER_SINGLY_LINKED_LIST.
         * The function accepts following parameters:
         *  1. INTEGER_SINGLY_LINKED_LIST llist
         *  2. INTEGER data
         *  3. INTEGER position
    
  • + 1 comment

    public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head, int data, int position) {

    SinglyLinkedListNode node = new SinglyLinkedListNode(data);
    if (head == null){
        return node;
    }
    
        if (position == 0){
            node.next = head;
            return node;
        }
    
        else {
            SinglyLinkedListNode current = head;
            for (int i = 0; i < position - 1; i++){
                current = current.next;
            }
    
            node.next = current.next;
            current.next = node;
            return head;
        }
    

    }

    • + 0 comments

      Before this you will need to remove the Result class and it's braces in order for code to compile.

  • + 0 comments
        public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position) {
            SinglyLinkedListNode newNode = new SinglyLinkedListNode(data);
    
            // If inserting at the head (position 0)
            if (position == 0) {
                newNode.next = llist;
                return newNode;
            }
            
            // Traverse the list to find the node before the desired position
            SinglyLinkedListNode current = llist;
            for (int i = 0; i < position - 1 && current != null; i++) {
                current = current.next;
            }
            
            // Insert the new node
            if (current != null) {
                newNode.next = current.next;
                current.next = newNode;
            }
            
            return llist;
        }
    
  • + 0 comments

    The starting boiler plate in Java needs to be refactored or else it will never compile.