Singly Linked Lists
Published: 2022-03-12 15:08:53

As I have already discussed in the [previous blog](https://dev.to/itsdr9/linked-lists-l5g) 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1647093689918/hQYmcfjDY.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1647094332833/p_CafA_SC.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1647094705081/K5ugfbZ6X.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1647095646269/84A5QmHAr.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1647096964243/Y9t_oL3xR.png)
We will continue the remaining operations in the next blog. Stay tuned :)