Skip to content
Home Tutorials Roadmaps Courses Classroom Training Blog
Log in Join free
Computer Science Beginner Free

Arrays & Linked Lists

Lesson 4 of 17 · Data Structures 24%
1. Introduction to CS
What is Computer Science?
6. Operating Systems
OS Fundamentals
7. Cybersecurity Basics
Security Fundamentals
8. Software Engineering
Software Development Life Cycle
9. Cloud Computing
Cloud Computing Basics
10. Dynamic Programming
Dynamic Programming Introduction
11. AI & Machine Learning
Introduction to AI & ML
12. Internet of Things
IoT Fundamentals
13. Blockchain Basics
How Blockchain Works

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).

Choose an answer to continue.

Interview Questions

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

Up next

Trees & Graphs

Lesson 5 of 17 · Data Structures