Insert a node at the head of a linked list

  • + 0 comments

    Here's a Python 3 code with O(1) time Complexity (Correct me if I'm wrong):

    def insertNodeAtHead(llist, data):
        newNode = SinglyLinkedListNode(data)
        if llist is None:
            llist = newNode
            return llist
        else:
            newNode.next = llist
            llist = newNode
        return llist