Insert a node at a specific position in a linked list

Sort by

recency

|

36 Discussions

|

  • + 0 comments

    kinda wish you could thumbs-down the questions that pretend to cater to some languages and just don't. I guess it's python again then...

    def insertNodeAtPosition(llist, data, position):
        newNode = SinglyLinkedListNode(data)
        if position == 0:
            newNode.next = llist
            return newNode
        insertAt = llist
        while position > 1:
            position-=1
            insertAt = insertAt.next
        newNode.next = insertAt.next
        insertAt.next = newNode
        return llist
    
  • + 0 comments

    My python solution

    def insertNodeAtPosition(llist, data, position):
        data = SinglyLinkedListNode(data)
        prev = None
        count = 0
        node = llist
        while count < position:
            prev = node
            node = node.next
            count += 1
        
        prev.next = data
        data = prev.next
        data.next = node
        
        return llist
    
  • + 0 comments
    def insertNodeAtPosition(llist, data, position):
        # Write your code here
        counter = 0
        root = SinglyLinkedList()
       
        next_node = llist
        while ( next_node is not None):
    
            if (counter == position):
                root.insert_node(data)
            else:
                temp = next_node
                root.insert_node(temp.data)
                next_node = temp.next
            counter += 1
        return root.head
    
  • + 0 comments

    Python 3

    However I look at this, the 'position 0' case seems special as you return a different 'head'. The simplest option I could see was to treat it as a separate case.

    I quite like the proposed naming and structure here of 'next_index' being the count, starting from 1, and running through the llist until you find the right point to do the insert... although maybe 'node_count' or something would be more technically correct than 'index'

    def insertNodeAtPosition(llist, data, position):
        
        new_node = SinglyLinkedListNode(data)
        
        if position == 0:
            new_node.next = llist
            return new_head
        
        this = llist
        next_index = 1
        while next_index != position:
            this = this.next
            next_index += 1
        
        new_node.next = this.next
        this.next = new_node
        return llist
    
  • + 0 comments

    c++

    SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) { SinglyLinkedListNode* newNode = new SinglyLinkedListNode(data);

    if (position == 0) {
        newNode->next = llist;
        return newNode;
    }
    
    SinglyLinkedListNode* temp = llist;
    while (--position > 0 && temp->next) {
        temp = temp->next;
    }
    
    SinglyLinkedListNode* tempNext = temp->next;
    temp->next = newNode;
    newNode->next = tempNext;
    
    return llist;
    

    }