Skip to content

Instantly share code, notes, and snippets.

@jamesshah
Last active October 21, 2019 19:04
Show Gist options
  • Save jamesshah/f6924cdba54ad270312856d40b8ec326 to your computer and use it in GitHub Desktop.
Save jamesshah/f6924cdba54ad270312856d40b8ec326 to your computer and use it in GitHub Desktop.
Linked List Implementation in Java
//Implementing Singly Linked List in Java
class LinkedList{
Node head; //Reference to the Node class to create head node of the list.
//Node class to create nodes. here we make static class so that main method
//can recoginze it.
static class Node{
int data;
Node next;
//Constructor
Node(int d){
data = d;
next = null;
}
}
//To print the data of all the nodes of the list.
public void printList(){
Node tempNode = head;
while (tempNode!= null) {
System.out.print(tempNode.data + " " );
tempNode = tempNode.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.head = new Node(5);
Node secondNode = new Node(6);
Node thirdNode = new Node(7);
//Connecting nodes of the list by adding Reference to the next node.
list.head.next = secondNode;
secondNode.next = thirdNode;
//Printing data of all the nodes of the list.
list.printList();
}
}
//Insertion operation in a Linked List.
//There are three possibilities for Insertion in a Linked List.
//1- Insertion at the start of the list.
//2- Insertion at the end of the list.
//3- Insertion after a specific node.
//Insertion at the start of the list.
public void insertionAtStart(int new_node_data){
Node new_node = new Node(new_node_data);
//Creating a new node and adding data into it.
//Adding reference of head node into new node.
new_node.next = head;
//And now new node becomes the head of the list.
head = new_node;
}
}
//Insertion at the end of the list.
public void insertionAtEnd(int new_node_data){
//Creating a new node and adding data into it.
Node new_node = new Node(new_node_data);
//Check if there the list is empty.
if(head == null){
head = new_node;
}
//else traverse through the whole list, untill we reach the last node.
else{
Node last = head;
while(last.next != null){
last = last.next;
}
last.next = new_node;
}
}
//Insertion after a specific node.
//Here we pass data of new node as well as previous node - after which the new
//node is to be inserted.
public void insertionAfterSpecificNode(Node prev_node, int new_node_data){
if(prev_node == null){
System.out.println("Error -- Given Previous Node is Null.");
return ;
}
Node new_node = new Node(new_node_data);
new_node.next = prev_node.next;
prev_node.next = new_node;
}
//Implementing Linked List with all three insertion possibilities in Java
class LinkedList{
Node head; //Reference to the Node class to create head node of the list.
//Node class to create nodes. here we make static class so that main method
//can recoginze it.
static class Node{
int data;
Node next;
//Constructor
Node(int d){
data = d;
next = null;
}
}
//Insertion at the start of the list.
public void insertionAtStart(int new_node_data){
Node new_node = new Node(new_node_data);
//Creating a new node and adding data into it.
//Adding reference of head node into new node.
new_node.next = head;
//And now new node becomes the head of the list.
head = new_node;
}
//Insertion at the end of the list.
public void insertionAtEnd(int new_node_data){
//Creating a new node and adding data into it.
Node new_node = new Node(new_node_data);
//Check if there the list is empty.
if(head == null){
head = new_node;
}
//else traverse through the whole list, untill we reach the last node.
else{
Node last = head;
while(last.next != null){
last = last.next;
}
last.next = new_node;
}
}
//Insertion after a specific node.
//Here we pass data of new node as well as previous node - after which the new
//node is to be inserted.
public void insertionAfterSpecificNode(Node prev_node, int new_node_data){
if(prev_node == null){
System.out.println("Error -- Given Previous Node is Null.");
return ;
}
Node new_node = new Node(new_node_data);
new_node.next = prev_node.next;
prev_node.next = new_node;
}
//To print the data of all the nodes of the list.
public void printList(){
Node tempNode = head;
while (tempNode!= null) {
System.out.print(tempNode.data + " " );
tempNode = tempNode.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
//Creating a head node for the list.
list.head = new Node(5);
//Inserting a new node at the end of the list.
list.insertionAtEnd(10);
//Insertinf a new node at the start of the list.
list.insertionAtStart(15);
//inserting a new node after second node in the list.
list.insertionAfterSpecificNode(list.head.next,7);
//Printing data of all the nodes of the list.
list.printList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment