DEV Community

Kristi
Kristi

Posted on

Singly linked lists

What is a linked list?

A linked list is a collection of objects called nodes that are randomly stored in memory.

What is a node?

Node is a combination of two different types:

  1. data stored at the particular address which can be char, int, float, string
  2. pointer to the address of the next node in memory The last node of the list contains a pointer to NULL

What is a singly linked list?

A singly linked list is a linked list that can be traversed only in the forward direction from head to the last node, tail.

Linked list

Each square is a node and numbers 5, 10, 15, 20 are the values of the nodes. 2000, 3000, 4000, NULL are the pointer to the address of the next node. So the first node which is called HEAD points to address 2000 which is the next node and so on. The last node points to NULL.

Why use linked list vs array?

  • Inserting data in the linked list is very quick but in the array, we have direct access to the element
  • The list is not required to be continuously present in the memory

Conclusion

This was a basic explanation of what is a singly linked list is. Hope you will find this useful and let me know in the comments if this post needs improvement.

Top comments (3)

Collapse
 
hesoyamm profile image
Kishore kunal • Edited

A widely unappreciated argument for ArrayList and against LinkedList is that LinkedLists are uncomfortable while debugging. The time spent by maintenance developers to understand the program, e.g. to find bugs, increases and IMHO does sometimes not justify the nanoseconds in performance improvements or bytes in memory consumption in enterprise applicatons. Sometimes (well, of course it depends on the type of applications), it's better to waste a few bytes but have an application which is more maintainable or easier to understand.

Collapse
 
pauljlucas profile image
Paul J. Lucas

This shouldn't be tagged #c -- there's no C code here.

Collapse
 
kristi profile image
Kristi

👍