Linked List Examples
Our visualization tool brings linked list operations to life through an intuitive graphical interface. Each node is represented as a rectangle containing the node's value, with arrows showing the connections between nodes. This sequential visualization makes it easy to understand list structures and operations at a glance.
A key feature of our tool is its dynamic node tracking. As your code traverses the list, whether during insertion, deletion, or search operations, the tool highlights the current node and its connections. This visual feedback makes it particularly useful for learning list manipulation concepts or debugging list-related algorithms.
Let's look at some common linked list algorithms and their visualizations:
Basic Operations
function insertAtHead(head, val) {
const newNode = new ListNode(val);
newNode.next = head;
return newNode;
}
function insertAtTail(head, val) {
const newNode = new ListNode(val);
if (!head) return newNode;
let current = head;
while (current.next) {
current = current.next;
}
current.next = newNode;
return head;
}
function insertAtPosition(head, val, position) {
if (position === 0) return insertAtHead(head, val);
const newNode = new ListNode(val);
let current = head;
let count = 0;
while (current && count < position - 1) {
current = current.next;
count++;
}
if (current) {
newNode.next = current.next;
current.next = newNode;
}
return head;
}
// Example usage:
let list = new ListNode(1);
list = insertAtTail(list, 2);
list = insertAtPosition(list, 3, 1);
function deleteHead(head) {
if (!head) return null;
return head.next;
}
function deleteTail(head) {
if (!head || !head.next) return null;
let current = head;
while (current.next.next) {
current = current.next;
}
current.next = null;
return head;
}
function deleteAtPosition(head, position) {
if (!head) return null;
if (position === 0) return head.next;
let current = head;
let count = 0;
while (current && count < position - 1) {
current = current.next;
count++;
}
if (current && current.next) {
current.next = current.next.next;
}
return head;
}
// Example usage:
let list = new ListNode(1);
list.next = new ListNode(2);
list.next.next = new ListNode(3);
list = deleteAtPosition(list, 1);
Generating interactive preview...
List Manipulation
function reverseList(head) {
let prev = null;
let current = head;
while (current) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
// Example usage:
let list = new ListNode(1);
list.next = new ListNode(2);
list.next.next = new ListNode(3);
list = reverseList(list);
function mergeSortedLists(l1, l2) {
const dummy = new ListNode(0);
let current = dummy;
while (l1 && l2) {
if (l1.val <= l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
current.next = l1 || l2;
return dummy.next;
}
// Example usage:
let list1 = new ListNode(1);
list1.next = new ListNode(3);
let list2 = new ListNode(2);
list2.next = new ListNode(4);
let merged = mergeSortedLists(list1, list2);
function detectCycle(head) {
if (!head || !head.next) return false;
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) {
return true;
}
}
return false;
}
// Example usage:
let list = new ListNode(1);
list.next = new ListNode(2);
list.next.next = new ListNode(3);
list.next.next.next = list.next; // Creates a cycle
detectCycle(list); // true
Generating interactive preview...
List Operations
function findMiddleNode(head) {
if (!head || !head.next) return head;
let slow = head;
let fast = head;
while (fast.next && fast.next.next) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
// Example usage:
let list = new ListNode(1);
list.next = new ListNode(2);
list.next.next = new ListNode(3);
findMiddleNode(list); // Returns node with value 2
// @ignore-function-tree
function reverseList(head) {
let prev = null;
let current = head;
while (current) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
function isPalindrome(head) {
if (!head || !head.next) return true;
// Find middle
let slow = head;
let fast = head;
while (fast.next && fast.next.next) {
slow = slow.next;
fast = fast.next.next;
}
// Reverse second half
let secondHalf = reverseList(slow.next);
// Compare first and second half
let firstHalf = head;
while (secondHalf) {
if (firstHalf.val !== secondHalf.val) {
return false;
}
firstHalf = firstHalf.next;
secondHalf = secondHalf.next;
}
return true;
}
// Example usage:
let list = new ListNode(1);
list.next = new ListNode(2);
list.next.next = new ListNode(2);
list.next.next.next = new ListNode(1);
isPalindrome(list); // true
function removeNthFromEnd(head, n) {
const dummy = new ListNode(0);
dummy.next = head;
let first = dummy;
let second = dummy;
// Advance first pointer by n+1 steps
for (let i = 0; i <= n; i++) {
first = first.next;
}
// Move both pointers until first reaches end
while (first) {
first = first.next;
second = second.next;
}
// Remove nth node
second.next = second.next.next;
return dummy.next;
}
// Example usage:
let list = new ListNode(1);
list.next = new ListNode(2);
list.next.next = new ListNode(3);
list = removeNthFromEnd(list, 2); // Removes node with value 2
Generating interactive preview...