Each node in a Singly Linked List contains a value and a reference to link to the next node.
Linked list organizes all the nodes in a sequence.
Example of an Singly Linked List :
Here the nodes are linked together.
// Definition for Singly Linked List
public class SinglyListNode {
int val;
SinglyListNode next;
SinglyListNode (int x){
val = x;
}
}
In most cases we use the head node to represent the whole list.
Operations :
In LinkedList we can't retrieve the element in constant time like arrays .
If we want to fetch ith element we have to traverse from the head node one by one .
It takes O(N) on average to visit an element where N is the length of the linked list.
For Example in above singly Linked List if we want to retrieve the element '1' we need to traverse one by one till it reaches element '1' from head node 5 using "next" .
So we may rise a Question what is the use of List when we can access elements in O(1) time using arrays ? (In Retrieval Lists are bad in performance compared to Arrays).
Linked List are useful and benefitted in insertion and deletion .
Add Operation :
https://techyield.blogspot.com/2019/06/singly-linked-list-add-operation.html
Delete Operation :
https://techyield.blogspot.com/2019/06/singly-linked-list-delete-operation.html
Linked list organizes all the nodes in a sequence.
Example of an Singly Linked List :
Here the nodes are linked together.
// Definition for Singly Linked List
public class SinglyListNode {
int val;
SinglyListNode next;
SinglyListNode (int x){
val = x;
}
}
In most cases we use the head node to represent the whole list.
Operations :
In LinkedList we can't retrieve the element in constant time like arrays .
If we want to fetch ith element we have to traverse from the head node one by one .
It takes O(N) on average to visit an element where N is the length of the linked list.
For Example in above singly Linked List if we want to retrieve the element '1' we need to traverse one by one till it reaches element '1' from head node 5 using "next" .
So we may rise a Question what is the use of List when we can access elements in O(1) time using arrays ? (In Retrieval Lists are bad in performance compared to Arrays).
Linked List are useful and benefitted in insertion and deletion .
Add Operation :
https://techyield.blogspot.com/2019/06/singly-linked-list-add-operation.html
Delete Operation :
https://techyield.blogspot.com/2019/06/singly-linked-list-delete-operation.html
No comments:
Post a Comment