DSA: Linked List
For Interviews
Linked lists are a popular topic in interviews, and understanding them thoroughly can be beneficial. They are frequently discussed in technical interviews, coding challenges, and assessments. Recruiters and interviewers often inquire about linked lists to assess a candidate's knowledge of data structures, algorithms, and problem-solving abilities. Mastering linked lists can help you tackle a wide range of programming problems efficiently.
- Day 1
Concept:
Assignments:
Given the head of a singly linked list, reverse the list, and return the reversed list.
Input: head = [1,2,3,4]
Output: [4,3,2,1]
- Day 2
Concept:
Assignments:
Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
The first node is considered odd, and the second node is even, and so on.
Note that the relative order inside both the even and odd groups should remain as it was in the input.
You must solve the problem in O(1) extra space complexity and O(n) time complexity.

- Day 3
Concept:
Assignments:
Given the head of a singly linked list, return true if it is a
palindrome or false otherwise.
Constraints:
The number of nodes in the list is in the range [1, 10^5].
0 <= Node.val <= 9
Examples
- Day 4
Concept:
Assignments:
Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle, and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
Implement the MyCircularQueue class:
MyCircularQueue(k) Initializes the object with the size of the queue to be k.
int Front() Gets the front item from the queue. If the queue is empty, return -1.
int Rear() Gets the last item from the queue. If the queue is empty, return -1.
boolean enQueue(int value) Inserts an element into the circular queue. Return true if the operation is successful.
boolean deQueue() Deletes an element from the circular queue. Return true if the operation is successful.
boolean isEmpty() Checks whether the circular queue is empty or not.
boolean isFull() Checks whether the circular queue is full or not.
You must solve the problem without using the built-in queue data structure in your programming language.
- Day 5
Concept:
Resources:
Assignments:
You are given the head of a linked list with n nodes.
For each node in the list, find the value of the next greater node. That is, for each node, find the value of the first node that is next to it and has a strictly larger value than it.
Return an integer array answer where answer[i] is the value of the next greater node of the ith node (1-indexed). If the ith node does not have a next greater node, set answer[i] = 0.

- Day 6
Concept:
Resources:
Assignments:
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Constraints:
The number of nodes in each linked list is in the range [1, 100].
0 <= Node.val <= 9
It is guaranteed that the list represents a number that does not have leading zeros.
Examples
Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]
- Day 7
Concept:
Resources:
Assignments:
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
Constraints:
The number of nodes in the list is in the range [0, 100].
0 <= Node.val <= 100
Example 1:
Input: head = [1,2,3,4]
Output: [2,1,4,3]
Example 2:
Input: head = []
Output: []
Example 3:
Input: head = [1]
Output: [1]
- Day 8
Concept:
Resources:
Assignments:
Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
Constraints:
The number of nodes in the list is in the range [0, 200].
-100 <= Node.val <= 100
-200 <= x <= 200
Example 1:
Input: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]
Example 2:
Input: head = [2,1], x = 2
Output: [1,2]
- Day 9
Concept:
Resources:
Assignments:
Given the head of a singly linked list, return the middle node of the linked list.
If there are two middle nodes, return the second middle node.
Constraints:
The number of nodes in the list is in the range [1, 100].
1 <= Node.val <= 100
Example 1:
Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node of the list is node 3.
Example 2:
Input: head = [1,2,3,4,5,6]
Output: [4,5,6]
Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
- Day 10
Concept:
Resources:
Assignments:
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Constraints:
The number of nodes in the list is sz.
1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
Example 1:
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Example 2:
Input: head = [1], n = 1
Output: []
Example 3:
Input: head = [1,2], n = 1
Output: [1]
- Day 11
Concept:
Resources:
Assignments:
You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list.
The middle node of a linked list of size n is the ⌊n / 2⌋th node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x.
For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.
Constraints:
The number of nodes in the list is in the range [1, 10^5].
1 <= Node.val <= 10^5
Example 1:
Input: head = [1,3,4,7,1,2,6]
Output: [1,3,4,1,2,6]
Explanation:
Since n = 7, node 3 with value 7 is the middle node.
We return the new list after removing this node.
Example 2:
Input: head = [1,2,3,4]
Output: [1,2,4]
Explanation:
For n = 4, node 2 with value 3 is the middle node.
Example 3:
Input: head = [2,1]
Output: [2]
Explanation:
For n = 2, node 1 with value 1 is the middle node.
Node 0 with value 2 is the only node remaining after removing node 1.
- Day 12
Concept:
Resources:
Assignments:
Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
Constraints:
The number of the nodes in the list is in the range [0, 10^4].
-10^5 <= Node.val <= 10^5
pos is -1 or a valid index in the linked-list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
- Day 13
- Visit a new URL
- Go back a certain number of steps in history
- Move forward a certain number of steps in history
BrowserHistory(string homepage)
Initializes the browser history with the givenhomepage.void visit(string url)
Visitsurlfrom the current page and clears all forward history.string back(int steps)
Move back up tostepsin history. Return the current URL after the move.string forward(int steps)
Move forward up tostepsin history. Return the current URL after the move.BrowserHistory browserHistory = new BrowserHistory("leetcode.com")browserHistory.visit("google.com")→ leetcode → googlebrowserHistory.visit("facebook.com")→ leetcode → google → facebookbrowserHistory.visit("youtube.com")→ leetcode → google → facebook → youtubebrowserHistory.back(1)→ youtube → facebook → returns "facebook.com"browserHistory.back(1)→ facebook → google → returns "google.com"browserHistory.forward(1)→ google → facebook → returns "facebook.com"browserHistory.visit("linkedin.com")→ clears forward history → facebook → linkedinbrowserHistory.forward(2)→ cannot go forward → returns "linkedin.com"browserHistory.back(2)→ linkedin → facebook → google → returns "google.com"browserHistory.back(7)→ back to "leetcode.com" (start) → returns "leetcode.com"- 1 ≤
homepage.length,url.length≤ 20 - 1 ≤
steps≤ 100 homepageandurlconsist of lowercase letters or '.'- At most 5000 operations will be made
- Use a dynamic list or linked list to store the visited URLs.
- Maintain a pointer to track the current page index.
- Truncate the forward list when a new visit is made.
Concept:
Resources:
Assignments:
Design a Browser History System
You are to design a simplified browser history system with the ability to:
Class Definition
Implement a class BrowserHistory with the following functionality:
Example Input & Output
Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]
Explanation:
Constraints
📝 Notes
- Day 14
-
AuthenticationManager(int timeToLive)— Constructs the manager and sets the lifetime of tokens. -
generate(string tokenId, int currentTime)— Generates a new tokentokenIdatcurrentTime. -
renew(string tokenId, int currentTime)— Renews the token if it is unexpired. If already expired, it is ignored. -
countUnexpiredTokens(int currentTime)— Returns the number of tokens that are still valid atcurrentTime. 1 ≤ timeToLive ≤ 10⁸1 ≤ currentTime ≤ 10⁸1 ≤ tokenId.length ≤ 5tokenIdcontains only lowercase letters- Each
generatecall has a uniquetokenId currentTimevalues are strictly increasing- Maximum
2000method calls total
Concept:
Resources:
Assignments:
There is an authentication system that works with authentication tokens.
For each session, the user receives a token that expires
timeToLive seconds after the
currentTime.
If a token is renewed, its expiry updates to be
timeToLive seconds after the (possibly new)
currentTime.
You must implement the AuthenticationManager class with the following methods:
Note: If a token expires exactly at time t, and an action is called at time t,
the expiration is considered to happen before the action.
Example
Input:
["AuthenticationManager", "renew", "generate", "countUnexpiredTokens", "generate", "renew", "renew", "countUnexpiredTokens"] [[5], ["aaa", 1], ["aaa", 2], [6], ["bbb", 7], ["aaa", 8], ["bbb", 10], [15]]
Output:
[null, null, null, 1, null, null, null, 0]
Explanation:
AuthenticationManager authenticationManager = new AuthenticationManager(5);
authenticationManager.renew("aaa", 1); // No token "aaa" exists at time 1
authenticationManager.generate("aaa", 2); // Generate token "aaa" at time 2
authenticationManager.countUnexpiredTokens(6); // "aaa" still valid → return 1
authenticationManager.generate("bbb", 7); // Generate token "bbb" at time 7
authenticationManager.renew("aaa", 8); // "aaa" expired at time 7 → ignored
authenticationManager.renew("bbb", 10); // Renew "bbb" to expire at 15
authenticationManager.countUnexpiredTokens(15); // "bbb" expires at 15 → return 0