Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials Computer Science Arrays & Linked Lists
Computer Science Beginner FREE

Arrays & Linked Lists

Lesson 1 of 17 Beginner Interactive

Arrays store elements in contiguous memory. Linked lists store elements with pointers connecting them.

Comparison

  • Array: O(1) access, O(n) insert/delete
  • Linked List: O(n) access, O(1) insert/delete

Syntax

COMPUTER-SCIENCE
# Array
arr = [1, 2, 3, 4, 5]
arr[2]  # O(1) access

# Linked List
# Node -> [data|next] -> [data|next] -> null
Linked List Implementation
PYTHON
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        current = self.head
        while current.next:
            current = current.next
        current.next = new_node

    def display(self):
        elements = []
        current = self.head
        while current:
            elements.append(current.data)
            current = current.next
        return elements

ll = LinkedList()
for val in [1, 2, 3, 4]:
    ll.append(val)
print(ll.display())

Practice

1
Exercise

When should you use a linked list over an array?

Answer
When you need frequent insertions/deletions and don't need random access.

Quick Quiz

1

What is the time complexity of accessing an array element?

Array access is O(1).

Interview Questions

An array that automatically resizes when capacity is exceeded. Python lists and Java ArrayLists are dynamic arrays.