Singly Linked List
Last updated
Last updated
public void insertFirst(int data) {
Node newNode = new Node(data);
newNode.next = head; // Setting current new node address pointer next to previous head
head = newNode; // Assigning the current new node as the head
}public void insertLast(int data) {
Node current = head; // Assign current as head node
Node newNode = new Node(data); // Create a new node
if(current == null) { // If SLL is empty,
head = newNode; // Head Node is the new node
} else { // If head node is not empty
while(current.next != null) { // Get to the last
current = current.next;
}
current.next = newNode; // Assign new node as last node next
}
}