What I'm trying to do is insert a node after the head. Insert at any position, and when I insert at head: I want the previous head to move to head.next.
class Node{
Node next;
Node previous;
int data;
public Node(int data){
this.data = data;
}
}
public class LinkedList {
Node head;
public Node push(int data){
Node newNode = new Node(data);
if(head == null){
newNode.next = head;
head = newNode;
}
else{
newNode.next = head.next;
head.next = new Node(data);
}
return head;
}
public Node insertAtEnd(int data){
Node temp = this.head;
while(temp!=null){
temp = temp.next;
}
return temp = new Node(data);
}
Main
LinkedList ll = new LinkedList();
ll.push(15);
ll.push(4);
ll.push(78);
ll.push(55);
ll.insertAtEnd(80);
ll.printList();
int s = ll.getSize();
System.out.println(s);
Code is only outputting certain Nodes and not all nodes in a list.