Insert a node at a specific position in a linked list

  • + 0 comments

    Go solution

    func insertNodeAtPosition(llist *SinglyLinkedListNode, data int32, position int32) *SinglyLinkedListNode {
    	// find pos
    	currNode := llist
    	for position > 1 {
    		position--
    		currNode = currNode.next
    	}
    
    	// insert node
    	newNode := &SinglyLinkedListNode{data: data}
    	newNode.next = currNode.next
    	currNode.next = newNode
    	return llist
    }