Singly Linked Lists

Singly Linked Lists

As I have already discussed in the previous blog that a node in a linked list contains the data element and the address of the next node. When the node of the linked list contains only the data element and the address to the next node these kinds of linked lists are known as singly-linked lists. Here is a representation of the singly linked list

linkedlist head.png The last node of the linked list points to null, which means it contains no address. So one node of the linked list contains:- a data element and address of the next node. So let's try to code this:

  • A linked list contains several nodes.

So we will create a linked list class, which will have a subclass for nodes. Suppose we created two nodes of the linked list where the first node contains the data element and the address to the second node and the second node contains the data element and it points to null because there is no node after that. image.png We can access the second node using the first node, but wait how will we access the first node? We need a pointer that will point to the first node of the linked list. So we will create a node which we will call 'head' to point to the first node of the linked list.

image.png Now we can easily access each node of the linked list, we can access first node using head, and second node using the first node. The above things can be coded like this:

public class LinkedList {

    private Node head;

    private class Node {
        private int value;
        private Node next;

    }
}

I assume that you have some familiarity with object-oriented programming beforehand. Now when we create a node of the linked list, we want to pass a value that will be the data element contained by the node. So will create a constructor of the node class for this purpose.

public Node(int value) {
            this.value = value;
            this.next= null;
        }

Now the whole code is given below:

public class LinkedList {

    private Node head;

    private class Node {
        private int value;
        private Node next;

        public Node(int value) {
            this.value = value;
            this.next= null;
        }

    }
}

Now let us look what are the operations we can perform on the linked list. There are 6 basic operations that can be performed on these linked lists, and these operations will make things crystal clear.

  1. Traversal
  2. Insertion
  3. Deletion
  4. Searching
  5. Sorting
  6. Reversal

Traversing the linked list

The most important point to consider in a linked list is:

If the head node is lost the whole list is lost.

In order to traverse the linked list, we will create a node and set it equal to the head node, which means it will also point to the first node of the list.

image.png When temp == null means we have reached the end of the linked list. Here is the code to traverse the linked list:

public void traverse() {
        Node temp = head;
        while (temp != null) {
            temp = temp.next;
        }
    }

image.png We will continue the remaining operations in the next blog. Stay tuned :)