Arrays, Linked Lists & Big-O Notation

8 min read PYTHON 4 views

Data Structures & Algorithmic Complexity

Efficient software requires choosing the optimal data structure. Big-O notation measures the time and space complexity of algorithms as the input size scales.

Arrays vs Linked Lists

Arrays provide O(1) random access by index but O(N) insertion in the middle. Linked lists provide O(1) dynamic insertion when a pointer is known, but require O(N) sequential traversal.

python Example
Run Code
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
        curr = self.head
        while curr.next:
            curr = curr.next
        curr.next = new_node

ll = LinkedList()
ll.append(10)
ll.append(20)
print("Linked list head:", ll.head.data)

Related Tutorials