Imagine you are taking part in a treasure hunt game. The first thing you’d have to do is to find the first clue which would lead you to the second clue and that would lead you to the third clue and so on. To get something in the middle, or at the end, the only way to get to it is to follow this list from the beginning (or to cheat 😉 ).

In technical language- A Linked List is a linear collection of data elements, whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence.

Where arrays are stored in contiguous memory locations, in linked lists each “node” is at a random location in the memory. Each element in a linked list is stored in the form of a node.

So a node is like a box having two compartments. One side of the box contains the Data while the other side contains the address(in other words, a link) of the next node.

Note: The following coding exercises have been uploaded in JAVA.

Coding exercise 1: Representation of a Linked List

``````public class Program001Linked_List_Intro{

class Node{
int data;
Node next;

// Constructor to create a new node
// Next is by default initialized
// as null
Node (int d) { data= d;}
}
}``````

Coding exercise 2: Creation of three nodes
``````//create three nodes in a linked list

static class Node{

int data;
Node next;

Node (int d){
data=d;
next=null;
}
}

public static void main ( String [] args){

Node second= new Node(2);
Node third = new Node(3);

second.next=third;
}

}``````

Coding exercise 3: Traverse of a Linked List

``````//Traverse a Linked List
static class Node{
int data;
Node next;

Node (int d) {
data= d;
next= null;
}
}

public void printlist (){

while(n!=null)
{
System.out.print(n.data + " ");
n=n.next;
}
}

public static void main (String [] args){

Node second= new Node (2);
Node third= new Node (3);

second.next=third;

llist.printlist();
}
}``````

Coding exercise 4: Insert Node in beginning

``````//adding new node at beginning of linked list
public class Program004insert_node_in_beginning {
static class Node{
int data;
Node next;

Node(int d)
{
data=d;
next= null;
}
}

public void printlist (){

while (n!=null){
System.out.print(n.data + " ");
n=n.next;
}

System.out.println();
}

public void insertinbeginning (int new_data){

Node new_node= new Node(new_data);

}

public static void main(String [] args){
Program004insert_node_in_beginning llist= new Program004insert_node_in_beginning ();

Node second= new Node(2);
Node third= new Node(3);

second.next=third;

llist.printlist();

llist.insertinbeginning(0);
llist.printlist();
}
}``````

Coding exercise 5: Insert Node in Middle

``````
//inserting a node in the middle of linked list
public class Program005insert_node_in_middle {

static class Node
{
int data;
Node next;

Node (int d)
{
data=d;
next= null;
}
}

public void printlist ()
{

while(n!=null)
{
System.out.print(n.data+ " ");
n=n.next;
}

System.out.println();
}

public void insertinmiddle (Node prev_node, int new_data)
{
Node new_node= new Node(new_data);

new_node.next=prev_node.next;
prev_node.next=new_node;
}

public static void main (String [] args)
{
Program005insert_node_in_middle llist = new Program005insert_node_in_middle ();
Node second = new Node (2);
Node third= new Node(4);

second.next=third;

llist.printlist();

llist.insertinmiddle(second,3 );

llist.printlist();
}

}
``````

Coding exercise 6: Insert Node at end

``````//inserting node at the end
public class Program006insert_node_at_end {

static class Node
{
int data;
Node next;

Node(int d)
{
data= d;
next=null;
}
}

public void printlist()
{

while(n!=null)
{
System.out.print(n.data+ " ");
n=n.next;
}

System.out.println();
}

public void insertatend (int new_data)
{
Node new_node= new Node(new_data);

while(n.next!=null)
{
n=n.next;
}

n.next=new_node;

}

public static void main( String [] args)
{
Program006insert_node_at_end llist= new Program006insert_node_at_end();

Node second= new Node(2);
Node third= new Node(3);

second.next=third;

llist.printlist();

llist.insertatend(4);
llist.printlist();
}

}
``````

Coding exercise 7: Delete node containing the number 1 (or anything else) as it’s data

``````
//delete node
public class Program007delete_node {

static class Node{

int data;
Node next;

Node (int d)
{
data=d;
next=null;
}
}

public void delete (int key)
{

if(temp!=null && temp.data==key)
{
return;
}

while(temp!=null && temp.data!=key)
{
prev=temp;
temp=temp.next;
}

if(temp==null)return;
prev.next=temp.next;
}

public void printlist()
{

while(n!=null)
{
System.out.print(n.data +" ");
n=n.next;
}

System.out.println();
}

public static void main(String [] args)
{
Program007delete_node llist= new Program007delete_node();
Node second = new Node(3);
Node third= new Node(1);
Node fourth= new Node(7);

second.next=third;
third.next=fourth;

llist.printlist();
llist.delete(1);
llist.printlist();
}

}
``````

Coding exercise 8: Delete nth node

``````//delete nth node
public class Program008delete_nth_node {

static class Node{
int data;
Node next;

Node (int d)
{
data=d;
next=null;
}
}

public void delete(int key)
{
int k=0;

while(temp!=null && k!=key)
{
prev=temp;
temp=temp.next;
k++;
}

if(temp==null)return;
prev.next=temp.next;

}

public void printlist()
{

while(n!=null)
{
System.out.print(n.data+ " ");
n=n.next;
}

System.out.println();
}

public static void main (String [] args)
{
Program008delete_nth_node llist= new Program008delete_nth_node();
Node second= new Node(2);
Node third= new Node(3);
Node fourth= new Node(4);

second.next=third;
third.next=fourth;
llist.printlist();
llist.delete(2);
llist.printlist();
}``````

Coding exercise 9: Delete Linked List

``````

static class Node{
int data;
Node next;

Node (int d)
{
data=d;
next=null;
}
}

public void delete()
{
}

public void printlist()
{
while(temp!=null)
{
System.out.print(temp.data+ " ");
temp=temp.next;
}
}

public static void main(String []  args)
{

Node second= new Node(2);
Node third= new Node(3);

second.next= third;

llist.printlist();
System.out.println(" will be deleted");

}

}
``````

Coding exercise 10: Finding Length of a linked list (Iterative Method)

``````
//find the length of a linked list (iterative method)

static class Node{
int data;
Node next;

Node(int d)
{
data=d;
next=null;
}
}

public int get_length()
{
int length=0;

while(n!=null)
{
length++;
n=n.next;
}

return length;
}

public void printlist()
{

while(n!=null)
{
System.out.print(n.data +" ");
n=n.next;
}
System.out.println();
}

public static void main ( String [] args)
{

Node second= new Node(2);
Node third= new Node(3);

second.next=third;

llist.printlist();
System.out.println("Length of linked list "+ llist.get_length());
}

}

``````

Coding Exercise 11: Finding length of a linked list (Recursive Method)

``````

static class Node{
int data;
Node next;

Node(int d)
{
data=d;
next=null;
}
}

public int find_length(Node node)
{
if(node==null)
return 0;
else
return 1+find_length(node.next);
}

public void printlist()
{

while(n!=null)
{
System.out.print(n.data+ " ");
n=n.next;
}

System.out.println();
}

public static void main (String [] args)
{
Node second= new Node(2);
Node third= new Node(3);

second.next=third;

llist.printlist();

}

}
``````