Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Latest commit

 

History

History
106 lines (78 loc) · 4.08 KB

Linkedlists.md

File metadata and controls

106 lines (78 loc) · 4.08 KB

Linked Lists

##Contiguous vs Linked Data Structures Data Structures can be classified as either Contiguous or linked, depending on whether they use arrays or pointers.

Contiguous Data Structures

Contiguous Data Structures are allocated using a single slice of memory. Contiguous data structures include arrays, heaps, and hash tables.

Linked Data Structures

Linked data structures are constructed using distinct chunks of memory and are tied together using pointers. These include trees, graphs, and linked lists.

##Linked Lists There are several different types of linked lists such as:

Singly Linked List vs Doubly Linked List

alt text

Circularly Linked List

Circularly Linked Lists have a tail that has a pointer pointing back to the first node. Useful for things like Round-Robin Queues.

alt text

Why use linked Lists?

alt text

Circularly Linked lists are sometimes convenient for certain problems
Linked Lists have Constant insertion time at the head "index", while dynamic arrays must resize
Linked Lists can be used to implement queues / stacks more efficiently
When You don't need to access an index of the list
When you might want to insert items in the middle of the list
You don't need random access to any elements
You don't know the size of your dataset

When to not use linked lists

You need random access to elements
You need constant time access to indexed elements
You need speed when iterating through elements. Linked Lists are slower in iteration because arrays use pointer arithmetic
Memory is a concern: Arrays are more memory efficient than linked lists. Each Node in a linked list requires data and a pointer.

The Linked List Class

 class Node(object):
 
   def __init__(self, data=None, next_node=None):
       self.data = data
       self.next = next_node

Iterating through a linked list

  def print_list(head):
      while(head != None):
          print head.data
          head = head.next

Add a node to the tail of a linked list

  def Insert(head, data):
      if(head == None):
          return Node(data,None)
      else:
          pointer = head
          while(head.next != None):
              head = head.next
          head.next = Node(data, None)
      return pointer

Keeping track of the Head Pointer

There are many problems where after conducting a manipulation, you have to return the entire linked list. This implies that you have to keep track of the head node. Make sure you keep a copy of the head node to return at the end of the problem.

Tortoise and Hare Strategy - Detecting a Cycle in Linked Lists

Given a linked list, how do you figure out if it has a cycle? Instantiate a slow and fast pointer alt text

Increment both pointers at different speeds alt text alt text

If they meet, you know that there is a cycle! alt text

Popular Linked List Questions

Iterate through a linked list
Get value of node N in linked list
Reverse a linked list
Insert Node at head of linked list
Insert Node at tail of linked list
Insert Node at Nth position of linked list
delete node at Nth position of linked list
Merge two sorted linked lists
Delete duplicate values in linked list
Detect cycle in linked list
Find whether or not two linked lists merge
Given two merged linked lists, find the merge node