Electronics & Telecommunication (University) is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Doubly and circular linked lists

Doubly linked lists add backward links for O(1) two-way updates; circular linked lists connect tail to head (no nulls) so rotations and some operations are simpler — both are used where flexible, constant-time insert/delete at known positions is required.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Doubly and circular linked lists are basic dynamic data structures used in system software and applications that need fast insertion and deletion at arbitrary positions (for example: navigation history, undo-redo, free-list in memory managers, and implementing deques). Knowing them helps you design data structures that avoid expensive array shifting and support efficient rotations and two-way traversal.

Key ideas

Start from first principles:

  • Linked list: a collection of nodes where each node stores data and link(s) (pointers) to other node(s). A linked list stores elements in heap-allocated nodes connected by pointers instead of contiguous memory.
  • Node: a data structure with a value field and pointer field(s). We call the pointer fields links.
  • Pointer / reference: a variable holding the memory address of a node. In C/C++ this is a pointer type (e.g. Node*).

Definitions specific to this topic:

  • Doubly linked list: a linked list where each node has two link fields: prev and next. For a non-circular doubly linked list, the head node has prev = NULL and the tail node has next = NULL. Any node x in the list satisfies: x->next->prev == x (when x->next != NULL) and x->prev->next == x (when x->prev != NULL).
  • Circular linked list (singly circular): a linked list where each node has a single next link and the last node's next points back to the head (no NULL next). There is no node with next == NULL. You can start at any node and traverse all nodes by following next repeatedly; after n steps you return to the same node.
  • Circular doubly linked list: a doubly linked list whose tail's next points to head and head's prev points to tail. There are no NULL prev/next pointers.

Invariants and assumptions to remember (state them when writing proofs or code):

  • Empty non-circular doubly list: head == NULL and tail == NULL.
  • Single-node non-circular doubly list: head == tail and head->prev == NULL and head->next == NULL.
  • Empty circular lists are usually represented by head == NULL. For a single-node circular list, node->next == node (and for circular doubly, node->prev == node too).

Describe diagrams to draw (when required in an exam):

  • For a non-circular doubly list (3 nodes): draw three boxes in a row, each box divided vertically into three fields labeled "prev | data | next". Draw arrows from each node's next field to the next node's data field, and arrows from each node's prev field to the previous node's data field. Label head pointing to the first node and tail pointing to the last. Mark head->prev = NULL and tail->next = NULL.
  • For a circular singly list (4 nodes): draw four boxes, each with two fields "data | next" (or one field if you prefer), draw arrows from each next to the following node, and an arrow from the last node back to the first. Label head at one node. Show traversal direction by arrowheads.

Core operations and their step-by-step algorithms (state preconditions and postconditions):

  1. Traversal (forward) of non-circular doubly list: set p = head; while p != NULL: visit p->data; p = p->next. (Pre: head is pointer to first node or NULL; Post: visited each node exactly once.)
  2. Insertion at head (non-circular doubly list):
    • Pre: head may be NULL (empty list) or non-NULL.
    • Steps: a) Create new node s with s->data = x. b) s->next = head; s->prev = NULL. c) If head != NULL then head->prev = s else tail = s (list was empty). d) head = s.
    • Post: new node is first; invariants maintained.
    • Time: O(1). 2a) Insertion at tail (non-circular doubly list):
    • Pre: tail may be NULL (empty list) or non-NULL.
    • Steps: a) Create new node s with s->data = x. b) s->prev = tail; s->next = NULL. c) If tail != NULL then tail->next = s else head = s (list was empty). d) tail = s.
    • Post: new node is last; invariants maintained.
    • Time: O(1).
  3. Insertion after a given node p (non-circular):
    • Pre: p is a node in list (not NULL).
    • Steps: a) Create s with s->data = x. b) s->next = p->next; s->prev = p. c) If p->next != NULL then p->next->prev = s else tail = s. d) p->next = s.
    • Time: O(1) (if p known).
  4. Deletion of a given node p (non-circular):
    • Pre: p is a node in the list.
    • Steps: a) If p->prev != NULL then p->prev->next = p->next else head = p->next. b) If p->next != NULL then p->next->prev = p->prev else tail = p->prev. c) free p.
    • Time: O(1) (if p known). Handle single-node case where head and tail become NULL.
  5. For circular lists, insertion and deletion are similar but update wrap-around links instead of NULL checks. Example: inserting node s after node p in a circular singly list:
    • s->next = p->next; p->next = s. If inserting when list empty: s->next = s and head = s.
    • Deleting node after p: to delete q = p->next, set p->next = q->next; if q == head, update head = q->next; if q == p (single node) head = NULL; then delete q.

Complexity facts (state assumptions):

  • Searching (finding a node by key) requires O(n) in all linked lists because random access is not available.
  • Given direct pointer/reference to the node of interest, insertion and deletion in doubly linked lists are O(1). For circular singly lists, deletion of a node is O(1) if you have pointer to the node's predecessor (or handle special cases).

Proof sketch (why deletion is O(1) in doubly list if node pointer provided): you only change constant number of pointers: p->prev->next and p->next->prev (and possibly head/tail). These are constant-time pointer assignments; no traversal is needed.

Formulas

(Here we list C/C++ syntax and small rules to remember.) struct Node { int data; Node* prev; Node* next; }; — defines a doubly linked list node type. Node* head = NULL; Node* tail = NULL; — initializing empty list pointers. Node* s = new Node{value, NULL, NULL}; — allocate and initialize node in C++ (verify your compiler supports brace-init for struct). delete p; — free node pointed to by p (after relinking). for (Node* p = head; p != NULL; p = p->next) { /* visit p->data */ } — forward traversal non-circular. if (head) { Node* p = head; do { /* visit p->data */ p = p->next; } while (p != head); } — circular singly traversal (special-case empty list). Time complexities: Search: O(n) — search by key in any linked list. Insertion (given node): O(1) — doubly linked list. Deletion (given node): O(1) — doubly linked list. (Each line: the meaning is the usual count of pointer operations; units: number of primitive operations.)

Worked examples

Example 1 — Doubly linked list program: insert at tail n elements, delete first occurrence of a value x, print forward and backward. C++ program (single-file, uses stdin/stdout):

#include <bits/stdc++.h>
using namespace std;
struct Node {
    int data;
    Node* prev;
    Node* next;
    Node(int v): data(v), prev(NULL), next(NULL) {}
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; if(!(cin>>n)) return 0;
    Node* head = NULL; Node* tail = NULL;
    for(int i=0;i<n;i++){
        int v; cin>>v;
        Node* s = new Node(v);
        if(!head) head = tail = s;
        else { tail->next = s; s->prev = tail; tail = s; }
    }
    int x; cin>>x; // delete first occurrence of x
    // find node
    Node* p = head;
    while(p && p->data != x) p = p->next;
    if(p){
        if(p->prev) p->prev->next = p->next; else head = p->next;
        if(p->next) p->next->prev = p->prev; else tail = p->prev;
        delete p;
    }
    // print forward
    cout<<"Forward:";
    for(Node* q = head; q; q = q->next) cout<<" "<<q->data;
    cout<<"\n";
    // print backward
    cout<<"Backward:";
    for(Node* q = tail; q; q = q->prev) cout<<" "<<q->data;
    cout<<"\n";
    return 0;
}

Sample input:

5
10 20 30 40 50
30

Exact output:

Forward: 10 20 40 50
Backward: 50 40 20 10

Example 2 — Circular singly linked list: build list with n elements, rotate left by k positions (efficient using circular links) and print. C++ program:

#include <bits/stdc++.h>
using namespace std;
struct Node { int data; Node* next; Node(int v): data(v), next(NULL) {} };

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; if(!(cin>>n)) return 0;
    Node* head = NULL; Node* tail = NULL;
    for(int i=0;i<n;i++){
        int v; cin>>v;
        Node* s = new Node(v);
        if(!head) head = tail = s;
        else { tail->next = s; tail = s; }
    }
    if(tail) tail->next = head; // make circular (if n>0)
    int k; cin>>k; // rotate left by k
    if(!head){ cout<<"Rotated list:"<<"\n"; return 0; }
    k = (n==0)?0:(k % n);
    // move head k steps
    for(int i=0;i<k;i++) head = head->next;
    // print list starting at head, exactly n elements
    cout<<"Rotated list:";
    Node* p = head;
    for(int i=0;i<n;i++){ cout<<" "<<p->data; p = p->next; }
    cout<<"\n";
    return 0;
}

Sample input:

5
1 2 3 4 5
2

Exact output:

Rotated list: 3 4 5 1 2

Example 3 — (Theory model answer) Describe and prove steps for deleting a node with known pointer p in a circular doubly linked list. Model answer (short): Given pointer p in a non-empty circular doubly list with head pointer known:

  • If p->next == p (single node), set head = NULL and free p.
  • Else, set p->prev->next = p->next; p->next->prev = p->prev; if head == p then head = p->next; free p. These steps update only pointers of p's neighbours and head, so are O(1). Invariants remain: the list stays circular and doubly linked.

Common mistakes

  • In circular lists, using traversal condition p != NULL instead of stopping after n steps or checking p != start — this leads to infinite loops.
  • Assuming deletion by value is O(1); it is O(n) because you must first search for the node by value.
  • For doubly linked lists, forgetting to set the new head->prev = NULL after inserting at head.
  • Using delete on nodes twice or failing to unlink before delete (causes dangling pointers if other code still refers to node).

For your exam

Types of questions you will see:

  • Definitions and short notes: define doubly linked list, circular linked list, compare advantages/disadvantages.
  • Derivations / step-by-step algorithms: show insertion at head/tail and deletion of given node (include diagrams and handle edge cases).
  • Programs: write C/C++ program implementing doubly linked list insertion/deletion/traversal or a circular list rotation.
  • Diagrams: draw node boxes with prev/data/next and arrows for head/tail; for circular show last->next->head.

What a full-marks answer includes:

  • Precise definition with invariants (where prev/next are NULL or point back to head as appropriate).
  • Correct, clearly ordered algorithm steps with preconditions and postconditions.
  • Diagrams labeled with head/tail and pointer arrows (show NULL or self-loop where relevant).
  • Time complexity analysis and explanation of edge cases.
  • For programs: working code, explanation of cases (empty, single node), sample input-output if asked.

Quick check

  1. In a non-circular doubly linked list, what two pointer updates are required to delete an internal node p (not head or tail)?
  2. How do you represent a single-node circular linked list's next pointer?
  3. What is the time complexity to insert after a given node p in a doubly linked list (assume p known)?

Answers: 1) p->prev->next = p->next and p->next->prev = p->prev. 2) node->next = node (self-loop). 3) O(1).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?