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
|
954 Discussions
|
Please Login in order to post a comment
My solution in python :)
Javascript
C solution:
SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) {
SinglyLinkedListNode ptr; ptr = (SinglyLinkedListNode)malloc(sizeof(SinglyLinkedListNode*)); if(ptr == NULL) { printf("empty list"); } ptr->data = data; ptr->next = llist; llist = ptr;
return llist; }
def insertNodeAtHead(llist, data): new_node = SinglyLinkedListNode(data) new_node.next = llist llist = new_node return llist
Java Solution:
static SinglyLinkedListNode insertNodeAtHead(SinglyLinkedListNode llist, int data) { SinglyLinkedListNode newNode = new SinglyLinkedListNode(data); if (llist == null) { return newNode; } else { newNode.next = llist; return newNode; } }