So, im trying to learn the implementation of linked list in java. I created a method to insert in the start but when i run it through main, i don't see any output returned.
class ListNode{
int data;
ListNode next;
ListNode(int data)
{
this.data = data;
this.next = null;
}
}
class Operations{
static ListNode head = null;
void insertStart(int num, ListNode head)
{
ListNode temp = new ListNode(num);
if (head == null)
{
head = temp;
}
else{
temp.next = head;
head = temp;
}
}
void display(ListNode head){
ListNode temp = head;
while (temp != null)
{
System.out.println(temp.data+"->");
temp = temp.next;
}
}
public static void main(String[] args) {
Operations l1 = new Operations();
l1.insertStart(5,head);
l1.insertStart(4, head);
l1.insertStart(3, head);
l1.insertStart(2, head);
l1.insertStart(1, head);
l1.display(head);
}
}
Can someone please help me, i can't seem to find the error. Everything seems perfect.