The previous lessons built, walked and edited singly linked lists. Doubly linked and circular lists use the same ideas with more pointers to keep right. A doubly linked insert or delete has to fix the links in both directions; a circular list has no null, so its edits must keep the ring closed. This lesson shows each operation step by step.
Walk to the node before the gap, curr. Then set the new node's own pointers first (prev to curr, next to curr's old next) — at that point nothing in the list has changed, so nothing can be lost. Then point the two neighbours at it: the old next's prev, and curr's next last. Inserting at index 0 is the special case: the new node becomes the head and its prev stays null.
Members only · free account
The rest of this lesson, with 6 more sections, 2 drawings, 3 dry-run simulators and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson