Module 3 · Linked List · Linked List Implementation and Operations

Design linked list

A dummy head node sits in front of index 0, so every index has a node before it — even 0. Two small helpers, getAtIndex and getSize, do all the walking, and each public method is a few lines on top of them.

The linked list starts with a dummy head node (-1) in front of index 0. The call stack shows the helper methods that do the walking.

Operation

Design a singly linked list with get(index), addAtHead(val), addAtTail(val), addAtIndex(index, val) and deleteAtIndex(index). Indices are 0-based; an invalid index returns -1 or does nothing. LeetCode 707 ↗