Insert a node at a specific position in a linked list

  • + 0 comments
    def insertNodeAtPosition(llist, data, position):
        # Write your code here
        data = SinglyLinkedListNode(data)
        if position == 0:
            data.next = llist
            return data
        
        current = llist
        for _ in range(position - 1):
            current = current.next
        
        data.next = current.next
        current.next = data
        
        return llist