Linked List

Read this in other languages: 简体中文, Русский, 日本語, Português, 한국어, Español,

In computer science, a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of data and a reference (in other words, a link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence during iteration. More complex variants add additional links, allowing efficient insertion or removal from arbitrary element references. A drawback of linked lists is that access time is linear (and difficult to pipeline). Faster access, such as random access, is not feasible. Arrays have better cache locality as compared to linked lists.

Linked List

Pseudocode for Basic Operations

Insert

  1. Add(value)
  2. Pre: value is the value to add to the list
  3. Post: value has been placed at the tail of the list
  4. n node(value)
  5. if head = ø
  6. head n
  7. tail n
  8. else
  9. tail.next n
  10. tail n
  11. end if
  12. end Add
  1. Prepend(value)
  2. Pre: value is the value to add to the list
  3. Post: value has been placed at the head of the list
  4. n node(value)
  5. n.next head
  6. head n
  7. if tail = ø
  8. tail n
  9. end
  10. end Prepend

Search

  1. Contains(head, value)
  2. Pre: head is the head node in the list
  3. value is the value to search for
  4. Post: the item is either in the linked list, true; otherwise false
  5. n head
  6. while n != ø and n.value != value
  7. n n.next
  8. end while
  9. if n = ø
  10. return false
  11. end if
  12. return true
  13. end Contains

Delete

  1. Remove(head, value)
  2. Pre: head is the head node in the list
  3. value is the value to remove from the list
  4. Post: value is removed from the list, true, otherwise false
  5. if head = ø
  6. return false
  7. end if
  8. n head
  9. if n.value = value
  10. if head = tail
  11. head ø
  12. tail ø
  13. else
  14. head head.next
  15. end if
  16. return true
  17. end if
  18. while n.next != ø and n.next.value != value
  19. n n.next
  20. end while
  21. if n.next != ø
  22. if n.next = tail
  23. tail n
  24. tail.next = null
  25. end if
  26. n.next n.next.next
  27. return true
  28. end if
  29. return false
  30. end Remove

Traverse

  1. Traverse(head)
  2. Pre: head is the head node in the list
  3. Post: the items in the list have been traversed
  4. n head
  5. while n != ø
  6. yield n.value
  7. n n.next
  8. end while
  9. end Traverse

Traverse in Reverse

  1. ReverseTraversal(head, tail)
  2. Pre: head and tail belong to the same list
  3. Post: the items in the list have been traversed in reverse order
  4. if tail != ø
  5. curr tail
  6. while curr != head
  7. prev head
  8. while prev.next != curr
  9. prev prev.next
  10. end while
  11. yield curr.value
  12. curr prev
  13. end while
  14. yield curr.value
  15. end if
  16. end ReverseTraversal

Complexities

Time Complexity

AccessSearchInsertionDeletion
O(n)O(n)O(1)O(n)

Space Complexity

O(n)

References