What is a Linked List?
A linked list, on the other hand, is a linear data structure where each element points to the next. It is more flexible than an array as it allows efficient insertions and deletions.
Pros of Linked Lists:
- Dynamic Size: Grow or shrink the list as needed.
- Easy Insertion/Deletion: Just change the pointers without shifting other elements.
Cons of Linked Lists:
- Slower Access: You have to start at the beginning and follow the links to find an item.
- More Memory: Each element requires extra space for the pointer(s).
Which is better linked list or array?
Linked lists and arrays have their own strengths and weaknesses. The choice between them depends on the specific requirements of the task at hand. Arrays are better when you need fast access to elements, while linked lists are better when you need to perform frequent insertions and deletions.