Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linked List: Overview

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Linked List

Overview:
A Linked List is a linear data structure, where elements are stored at non-
contiguous locations. A Linked list is like a chain made of nodes and the links are
pointers.
Pointers are the connections that hold the pieces of linked structures together.
Pointers represent the address of a location in memory.

Doubly Linked List: each node has two pointer, this allows you to go
backwards and forward rather than one direction way in a linked list

Circularly Linked List, end node points to the front node

Why Linked List?


They can be used to implement other common abstract data types, including
lists, stacks, queues

preferred data structures over arrays because of elements can be easily


inserted or removed without reallocation or reorganization of the entire
structure

Pros & Cons


Advantages of Linked List Disadvantages of Linked List

Linked List 1
Dynamic Size (there is no need to Very Slow when it comes to
ever resize a linked list) Access and Search we have to
iterate over each element
Quick Insertion/ Deletion of Nodes
sequentially starting from the first
because you just change the
node
pointers of each node to
insert/delete Extra space for pointer

Big O Analysis

Big
Operation Explanation
O

Insertion O1 you just change the pointers of each node to insert

Deletion O1 you just change the pointers of each node to delete

we have to iterate over each element sequentially starting from the


Access O(n)
first node to access the target node

we have to iterate over each element sequentially starting from the


Search O(n)
first node to access the target node

Code Implementation

# https://www.codefellows.org/blog/implementing-a-singly-linked-list-in-python/
class Node(object):
def __init__(self, data=None, next_node=None):
self.data = data
self.next_node = next_node

def get_data(self):
return self.data

def get_next(self):
return self.next_node

def set_next(self, new_next):


self.next_node = new_next

class LinkedList(object):
def __init__(self, head=None):

Linked List 2
self.head = head

def insert_node():
new_node = Node(data)
new_node.set_next(self.head)
self.head = new_node

Techniques
Runner Technique: iterate through the linked list with two pointers
simultaneously, with one ahead of the other. The "fast" node might be ahead by a
fixed amount, or it might be hopping multiple nodes for each one node that the
"slow" node iterates through.

Linked List 3

You might also like