Questions tagged [linked-list]

A linked list is a data structure where the list elements are not necessarily stored sequentially but rather each element contains a reference to the next (and optionally the previous) element in the list. This tag should be used with additional tags indicating the programming language being used ([c], [c++], [java], etc.) and any libraries or plug-ins being used such as [c++-standard-library]. The post itself should contain source code for the issue.

A linked list is a data structure in which the elements contain references to the next (and optionally the previous) element. Linked lists offer O(1) insert after and removal of any element with known memory location, O(1) list concatenation, and O(1) access at the front (and optionally back) positions as well as O(1) next element access. Random access and random index insertion/removal have O(n) complexity and are usually unimplemented.

Types

Lists are the canonical recursive (or inductive) data type.

Specific Implementations

Almost every programming language provides support for list data structures. They are particularly common in functional languages, because their inductive properties lend themselves to elegant iterative and recursive abstractions.

How they work

Linked lists are made up of nodes, each node takes up a space in memory. The order of nodes in memory is irrelevant, this is because linked lists use pointers. Pointers, as the name suggests, points to the memory address of a node. There is a start pointer which points to the first node, a null pointer for the last node which shows the end of the list. Each node also has one or two pointers depending on whether its a singly or doubly linked list.

Performance

The flexibility of linked lists is somewhat offset by the complexity of some operations on lists (accessing an element at index n in the list is O(n), while for arrays it is O(1)). In practice, they are a very disadvantageous structure for random-access-based algorithms. The performance of lists and arrays is of the same order (O) for operations that deal with every element of the list in order.

However, they have good performance when dealing with large lists in which random access based on indices is not required but the removal of elements at known positions in memory is.

See also

17452 questions
3293
votes
33 answers

When to use LinkedList over ArrayList in Java?

I've always been one to simply use: List names = new ArrayList<>(); I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code. When should LinkedList be used over ArrayList and…
sdellysse
  • 36,011
  • 9
  • 23
  • 24
452
votes
25 answers

How to detect a loop in a linked list?

Say you have a linked list structure in Java. It's made up of Nodes: class Node { Node next; // some user data } and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the…
jjujuma
  • 19,905
  • 12
  • 39
  • 45
414
votes
15 answers

When should I use a List vs a LinkedList

When is it better to use a List vs a LinkedList?
Jonathan Allen
  • 63,625
  • 65
  • 234
  • 426
213
votes
9 answers

How is Python's List Implemented?

Is it a linked list, an array? I searched around and only found people guessing. My C knowledge isn't good enough to look at the source code.
Greg
  • 39,830
  • 86
  • 217
  • 286
207
votes
34 answers

Array versus linked-list

Why would someone want to use a linked-list over an array? Coding a linked-list is, no doubt, a bit more work than using an array and one may wonder what would justify the additional effort. I think insertion of new elements is trivial in a…
Onorio Catenacci
  • 14,322
  • 12
  • 75
  • 122
198
votes
15 answers

When to use a linked list over an array/array list?

I use a lot of lists and arrays but I have yet to come across a scenario in which the array list couldn't be used just as easily as, if not easier than, the linked list. I was hoping someone could give me some examples of when the linked list is…
faceless1_14
  • 6,794
  • 9
  • 28
  • 33
193
votes
7 answers

Why does std::list::reverse have O(n) complexity?

Why does the reverse function for the std::list class in the C++ standard library have linear runtime? I would think that for doubly-linked lists the reverse function should have been O(1). Reversing a doubly-linked list should just involve…
Curious
  • 19,352
  • 6
  • 45
  • 114
183
votes
8 answers

Why is ArrayDeque better than LinkedList

I am trying to to understand why Java's ArrayDeque is better than Java's LinkedList as they both implement Deque interface. I hardly see someone using ArrayDeque in their code. If someone sheds more light into how ArrayDeque is implemented, it…
Cruel
  • 1,833
  • 2
  • 12
  • 4
183
votes
28 answers

Python Linked List

What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by '(1 2 3 4 5). Python's lists, [1, 2, 3, 4, 5], and tuples, (1, 2, 3, 4, 5), are not, in fact, linked lists, and linked lists have some nice…
Claudiu
  • 206,738
  • 150
  • 445
  • 651
180
votes
22 answers

Explain how finding cycle start node in cycle linked list work?

I understand that Tortoise and Hare's meeting concludes the existence of loop, but how does moving tortoise to beginning of linked list while keeping the hare at meeting place, followed by moving both one step at a time make them meet at starting…
Passionate programmer
  • 5,328
  • 10
  • 34
  • 43
136
votes
6 answers

How do I create a Linked List Data Structure in Java?

What's the best way to make a linked list in Java?
Lance Fisher
  • 25,328
  • 20
  • 91
  • 121
123
votes
14 answers

Why is inserting in the middle of a linked list O(1)?

According to the Wikipedia article on linked lists, inserting in the middle of a linked list is considered O(1). I would think it would be O(n). Wouldn't you need to locate the node which could be near the end of the list? Does this analysis not…
Rob Sobers
  • 19,173
  • 22
  • 78
  • 107
121
votes
11 answers

Why do linked lists use pointers instead of storing nodes inside of nodes

I've worked with linked lists before extensively in Java, but I'm very new to C++. I was using this node class that was given to me in a project just fine class Node { public: Node(int data); int m_data; Node *m_next; }; but I had one…
m0meni
  • 14,160
  • 14
  • 66
  • 120
120
votes
15 answers

Under what circumstances are linked lists useful?

Most times I see people try to use linked lists, it seems to me like a poor (or very poor) choice. Perhaps it would be useful to explore the circumstances under which a linked list is or is not a good choice of data structure. Ideally, answers would…
Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
112
votes
35 answers

How to reverse a singly linked list using only two pointers?

I wonder if there exists some logic to reverse a singly-linked list using only two pointers. The following is used to reverse the single linked list using three pointers namely p, q, r: struct node { int data; struct node *link; }; void…
Madhan
  • 2,491
  • 7
  • 24
  • 22
1
2 3
99 100