Insert a node at the head of a linked list

Sort by

recency

|

974 Discussions

|

  • + 0 comments

    The C# doesn't compile. Seeing this error even without my code:

    Solution.cs(32,25): warning CS8625: Cannot convert null literal to non-nullable reference type.

  • + 0 comments

    compile failure in C# code

  • + 0 comments

    Here is my c++ solution you watch vide explanation here : https://youtu.be/COnfpdhOlN8

    SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) {
       SinglyLinkedListNode* new_node = new SinglyLinkedListNode(data);
       new_node -> next = llist;
       return new_node;
    }
    
  • + 0 comments
    def insertNodeAtHead(head, data):
        node = SinglyLinkedListNode(data)
        node.next = head
        
        return node
    
  • + 1 comment

    Heres a really simple and clear code in python

    def insertNodeAtHead(head, data):
        temp = SinglyLinkedListNode(data)
        if not head:
            return temp
        else:
            temp.next = head
            head = temp
            return head