Insert a node at the head of a linked list

Sort by

recency

|

988 Discussions

|

  • + 0 comments

    On the C# variant you have an error in line 83 that prevents code to be evaluated. PrintSinglyLinkedList(llist->head, "\n", textWriter); I think it should be: PrintSinglyLinkedList(llist_head, "\n", textWriter); Not all the time java code works in C# :D

  • + 0 comments

    Java Solution we want to insert the node head of the sll(sll Node,int data) { sll new head= new sllNode(data); if(list!=NULL) { Node.next=list return new head }

  • + 0 comments

    Java Solution we want to insert the node head of the sll(sll Node,int data) { sll new head= new sllNode(data); if(list!=NULL) { Node.next=list return new head }

  • + 0 comments

    My Java solution with o(1) time complexity and o(1) space complexity:

    static SinglyLinkedListNode insertNodeAtHead(SinglyLinkedListNode llist, int data) {
            SinglyLinkedListNode newHead = new SinglyLinkedListNode(data);
            newHead.next = llist;
            return newHead;
        }
    
  • + 0 comments

    python 3

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