01. Introduction to LinkedLists

What is a Linked List?

A linked list is a data structure that consists of a group of nodes that together represent a sequence.

Each node is composed of a generic data element and a reference to the next node in the sequence.

Comparison to Arrays

  • Arrays can be traversed randomly, while linked lists must be traversed sequentially.
  • Linked list can grow dynamically, while arrays must define a size before its instantiation.
  • Because arrays have fixed sizes, we must know the upper limit of the number of elements in advance.
  • Memory allocation for an array is compile time and linked list is run time.
  • Inserting elements into an array is expensive since we have to shift all elements coming after the insertion node over one. For linked lists, however, you can simply insert a node by reconfiguring the node coming before it.
  • Linked lists allow for dynamic resizing, making it easy to add more data.

Types of Linked Lists

There are several types of linked lists, but we'll begin by looking at the simplest - the singly-linked list.

A singly-linked list has only a NEXT pointer, which points to the next node in sequence. A doubly-linked list would have an additional PREV pointer that points to the previous node.

There is also a circular linked list, in which one next NEXT pointer points to a previous element.

A linked list made up of nodes.

The linking of a group of NODE objects is what gives us a linked list. Each NEXT pointer connects one node to the next. If the node is at the end, its NEXT pointer will point to NULL.

Head and Tail

Additionally, there are two more pointers in a linked list. The HEAD of the linked list holds the first node in the list, while the TAIL holds the last.

Performance

OperationAverageWorst
Space O(n) O(n)
Lookup O(n) O(n)
Insert O(n) O(n)
Delete O(n) O(n)

Learn Enterprise Java Development for a Bright Career

Head First Java

Learn Enterprise Java Development for a Bright Career Try Java

Jump start your Java education with Head First Java! This book provides clean diagram examples, with text that is easy-to-understand in an almost too-casual language. Great for anyone new to Java or programming in general.

$ Check price
44.9544.95Amazon 4.5 logo(567+ reviews)

More Java resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Ad