We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Insert a node at the head of a linked list
Insert a node at the head of a linked list
Sort by
recency
|
985 Discussions
|
Please Login in order to post a comment
My Java solution with o(1) time complexity and o(1) space complexity:
python 3
Here is my c++ solution you watch vide explanation here : https://youtu.be/COnfpdhOlN8
Java Solution
static SinglyLinkedListNode insertNodeAtHead(SinglyLinkedListNode llist, int data) { SinglyLinkedListNode node = new SinglyLinkedListNode(data); if (llist != null){ node.next = llist; } llist = node; return llist; }
Just a warning to anyone writing this in C# c sharp: there is an error in the main function that makes solving this problem using C# impossible.
PrintSinglyLinkedList(llist->head, "\n", textWriter);
should be
PrintSinglyLinkedList(llist.head, "\n", textWriter);