Insert a node at a specific position in a linked list

  • + 0 comments

    Updated python solution

    def insertNodeAtPosition(llist, data, position):
        if llist is None:
            return SinglyLinkedListNode(data, None)
            
        current = llist
        for i in range(position):
            prev = current
            current = current.next
            
        Node = SinglyLinkedListNode(data)
        prev.next = Node
        Node.next = current
        
        return llist