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
109
votes
27 answers

Check if two linked lists merge. If so, where?

This question may be old, but I couldn't think of an answer. Say, there are two lists of different lengths, merging at a point; how do we know where the merging point is? Conditions: We don't know the length We should parse each list only once.
rplusg
  • 3,358
  • 6
  • 30
  • 44
103
votes
32 answers

Reversing a linked list in Java, recursively

I have been working on a Java project for a class for a while now. It is an implementation of a linked list (here called AddressList, containing simple nodes called ListNode). The catch is that everything would have to be done with recursive…
sdellysse
  • 36,011
  • 9
  • 23
  • 24
100
votes
13 answers

What's the fastest algorithm for sorting a linked list?

I'm curious if O(n log n) is the best a linked list can do.
Dirk
  • 6,445
  • 13
  • 47
  • 72
85
votes
4 answers

When to use HashMap over LinkedList or ArrayList and vice-versa

What is the reason why we cannot always use a HashMap, even though it is much more efficient than ArrayList or LinkedList in add,remove operations, also irrespective of the number of the elements. I googled it and found some reasons, but there was…
user517491
75
votes
2 answers

Why does cache locality matter for array performance?

In the following blog there is a statement about the advantage of arrays over linked lists: Arrays have better cache locality that can make a pretty big difference in performance. What does that mean? I don't understand how cache locality can…
Vaibhav Mishra
  • 9,447
  • 11
  • 41
  • 56
72
votes
10 answers

Binary Trees vs. Linked Lists vs. Hash Tables

I'm building a symbol table for a project I'm working on. I was wondering what peoples opinions are on the advantages and disadvantages of the various methods available for storing and creating a symbol table. I've done a fair bit of searching and…
benmcredmond
  • 1,662
  • 2
  • 15
  • 22
68
votes
10 answers

Where can I see the source code of the Sun JDK?

I want to have a look at how Java implements LinkedList. Where should I go to look at the source code?
ashokgelal
  • 74,448
  • 25
  • 67
  • 83
66
votes
13 answers

Finding loop in a singly linked-list

How can I detect that whether a singly linked-list has loop or not?? If it has loop then how to find the point of origination of the loop i.e. the node from which the loop has started.
Jainendra
  • 23,305
  • 30
  • 116
  • 165
65
votes
9 answers

ArrayList Vs LinkedList

I was following a previous post on this that says: For LinkedList get is O(n) add is O(1) remove is O(n) Iterator.remove is O(1) For ArrayList get is O(1) add is O(1) amortized, but O(n) worst-case since the array must be resized and…
Vicky
  • 4,980
  • 17
  • 52
  • 78
65
votes
17 answers

Creating a very simple linked list

I am trying to create a linked list just to see if I can, and I am having trouble getting my head around it. Does anyone have an example of a very simple implementation of Linked list using C#? All the examples I have found so far are quite…
Shane
  • 1,071
  • 1
  • 11
  • 16
63
votes
8 answers

Why increase pointer by two while finding loop in linked list, why not 3,4,5?

I had a look at question already which talk about algorithm to find loop in a linked list. I have read Floyd's cycle-finding algorithm solution, mentioned at lot of places that we have to take two pointers. One pointer( slower/tortoise ) is…
GG.
  • 2,537
  • 5
  • 23
  • 32
63
votes
28 answers

How to find nth element from the end of a singly linked list?

The following function is trying to find the nth to last element of a singly linked list. For example: If the elements are 8->10->5->7->2->1->5->4->10->10 then the result is 7th to last node is 7. Can anybody help me on how this code is working or…
Jony
  • 6,352
  • 19
  • 56
  • 69
62
votes
7 answers

Interview: Remove Loop in linked list - Java

I was asked this question in interview: "How to detect the loop in linked list?", I solved this but immediately the interviewer asked me how do I remove the loop in a linked list. I fumbled. So any pointers on how to solve this, may be pseudo code,…
SuperMan
  • 3,396
  • 10
  • 43
  • 49
62
votes
8 answers

Using pointers to remove item from singly-linked list

In a recent Slashdot Interview Linus Torvalds gave an example of how some people use pointers in a way that indicates they don't really understand how to use them correctly. Unfortunately, since I'm one of the people he's talking about, I also…
codebox
  • 18,210
  • 7
  • 54
  • 77
61
votes
9 answers

Yield Return In Java

I've created a linked list in java using generics, and now I want to be able to iterate over all the elements in the list. In C# I would use yield return inside the linked list while going over the list of elements contained in the list. How would I…
Kasper Holdum
  • 11,655
  • 4
  • 41
  • 72