Day 18: Linked List Interview Problems 🐍

1. Implement a Singly Linked List

Design a Singly Linked List class from scratch. It should support the fundamental operations: insertion, deletion, searching for a value, and traversing the list.

Examples

# Assume `ll` is an instance of your LinkedList class
ll.insert_at_end(10)
ll.insert_at_end(20)
ll.insert_at_end(30)
ll.traverse()       # Output: 10 -> 20 -> 30

ll.search(20)       # Output: True
ll.delete_node(20)
ll.traverse()       # Output: 10 -> 30
ll.search(20)       # Output: False

Constraints & Edge Cases

2. Reverse a Linked List

Given the `head` of a singly linked list, reverse the list in-place and return the new head.

Examples

Input:  head = 1 -> 2 -> 3 -> 4 -> 5 -> None
Output: 5 -> 4 -> 3 -> 2 -> 1 -> None

Constraints & Edge Cases

3. Detect a Cycle in a Linked List

Given the `head` of a linked list, determine if it has a cycle. A cycle exists if some node in the list can be reached again by continuously following the `next` pointer.

Examples

Input:  head = 3 -> 2 -> 0 -> -4 -> (points back to 2)
Output: True

Input:  head = 1 -> 2 -> None
Output: False

Constraints & Edge Cases

4. Merge Two Sorted Linked Lists

You are given the heads of two sorted linked lists, `list1` and `list2`. Merge them into a single, sorted linked list and return the head of the merged list.

Examples

Input:  list1 = 1 -> 2 -> 4, list2 = 1 -> 3 -> 4
Output: 1 -> 1 -> 2 -> 3 -> 4 -> 4

Constraints & Edge Cases

5. Remove N-th Node From End of List

Given the `head` of a linked list and an integer `n`, remove the $n$-th node from the end of the list and return its head.

Examples

Input:  head = 1 -> 2 -> 3 -> 4 -> 5, n = 2
Output: 1 -> 2 -> 3 -> 5

Input:  head = 1, n = 1
Output: None (empty list)

Constraints & Edge Cases

6. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order. Add the two numbers and return the sum as a new linked list.

Examples

Input:  l1 = 2 -> 4 -> 3, l2 = 5 -> 6 -> 4
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

Constraints & Edge Cases

7. Palindrome Linked List

Given the `head` of a singly linked list, return `true` if it is a palindrome and `false` otherwise.

Examples

Input:  1 -> 2 -> 2 -> 1
Output: True

Input:  1 -> 2
Output: False

Constraints & Edge Cases

8. Intersection Node of Two Linked Lists

Given the heads of two singly linked-lists `headA` and `headB`, return the node at which the two lists intersect. If they do not intersect, return `null`.

Examples

# listA = 4 -> 1 -> 8 -> 4 -> 5
# listB = 5 -> 6 -> 1 -> 8 -> 4 -> 5
# The lists intersect at the node with value 1.
Output: Reference to the node with value 1

Constraints & Edge Cases

9. Reverse Nodes in k-Group

Given a linked list, reverse the nodes of the list `k` at a time and return the modified list. `k` is a positive integer and is less than or equal to the length of the list.

Examples

Input:  head = 1 -> 2 -> 3 -> 4 -> 5, k = 2
Output: 2 -> 1 -> 4 -> 3 -> 5

Input:  head = 1 -> 2 -> 3 -> 4 -> 5, k = 3
Output: 3 -> 2 -> 1 -> 4 -> 5

Constraints & Edge Cases

10. Copy List with Random Pointer

Construct a deep copy of a linked list where each node contains a `val`, a `next` pointer, and an additional `random` pointer which can point to any node in the list or `null`.

Examples

# The input is a list of nodes where each node is represented as [val, random_index].
Input:  head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]

Constraints & Edge Cases

Note from Your Interviewer 📝