I am working on a basic Hackerrank problem where we append an element to the end of the linked list.
/*
Insert Node at the end of a linked list
head pointer input could be NULL as well for empty list
Node is defined as
class Node {
int data;
Node next;
}
*/
Node Insert(Node head,int data) {
if(head == null) {
Node node = new Node();
head = node;
head.data = data;
head.next = null;
return head;
}
while(head != null) {
head = head.next;
}
head.data = data;
head.next = null;
return head;
}
For some reason, this solution does not compile. I was looking problems other people solved, and they used a temporary node in the non-empty linked list solutions.