feat: populate note files with problem descriptions and code stubs

Add populate-notes.mjs that fetches problem descriptions and
Python/C++ code stubs from LeetCode's GraphQL API. Populated
all 197 NeetCode 150 note files with:
- Problem description (examples, constraints)
- Python code stub (function signature)
- C++ code stub (function signature + includes)

API responses cached in leetcode/.cache/leetcode/ for instant re-runs.
This commit is contained in:
2026-06-01 17:22:07 +08:00
parent e798e449bd
commit 1dec88aaf2
198 changed files with 10459 additions and 534 deletions
@@ -1,18 +1,79 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0002. Add Two Numbers :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0002. Add Two Numbers][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0002. Add Two Numbers][0002. Add Two Numbers]]
:END:
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.
*Example 1:*
#+begin_src
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
#+end_src
*Example 2:*
#+begin_src
Input: l1 = [0], l2 = [0]
Output: [0]
#+end_src
*Example 3:*
#+begin_src
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
#+end_src
*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.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
#+end_src
** TODO C++
#+begin_src cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
}
};
#+end_src
@@ -1,18 +1,80 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0019. Remove Nth Node From End of List :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0019. Remove Nth Node From End of List][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0019. Remove Nth Node From End of List][0019. Remove Nth Node From End of List]]
:END:
Given the ~head~ of a linked list, remove the ~n^{th}~ node from the end of the list and return its head.
*Example 1:*
#+begin_src
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
#+end_src
*Example 2:*
#+begin_src
Input: head = [1], n = 1
Output: []
#+end_src
*Example 3:*
#+begin_src
Input: head = [1,2], n = 1
Output: [1]
#+end_src
*Constraints:*
- The number of nodes in the list is ~sz~.
- ~1 <= sz <= 30~
- ~0 <= Node.val <= 100~
- ~1 <= n <= sz~
*Follow up:* Could you do this in one pass?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
#+end_src
** TODO C++
#+begin_src cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
}
};
#+end_src
@@ -1,18 +1,80 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0021. Merge Two Sorted Lists :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0021. Merge Two Sorted Lists][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0021. Merge Two Sorted Lists][0021. Merge Two Sorted Lists]]
:END:
You are given the heads of two sorted linked lists ~list1~ and ~list2~.
Merge the two lists into one *sorted* list. The list should be made by splicing together the nodes of the first two lists.
Return /the head of the merged linked list/.
*Example 1:*
#+begin_src
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
#+end_src
*Example 2:*
#+begin_src
Input: list1 = [], list2 = []
Output: []
#+end_src
*Example 3:*
#+begin_src
Input: list1 = [], list2 = [0]
Output: [0]
#+end_src
*Constraints:*
- The number of nodes in both lists is in the range ~[0, 50]~.
- ~-100 <= Node.val <= 100~
- Both ~list1~ and ~list2~ are sorted in *non-decreasing* order.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
#+end_src
** TODO C++
#+begin_src cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
}
};
#+end_src
@@ -1,18 +1,92 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0023. Merge K Sorted Lists :hard:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0023. Merge K Sorted Lists][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0023. Merge K Sorted Lists][0023. Merge K Sorted Lists]]
:END:
You are given an array of ~k~ linked-lists ~lists~, each linked-list is sorted in ascending order.
/Merge all the linked-lists into one sorted linked-list and return it./
*Example 1:*
#+begin_src
Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: The linked-lists are:
[
1->4->5,
1->3->4,
2->6
]
merging them into one sorted linked list:
1->1->2->3->4->4->5->6
#+end_src
*Example 2:*
#+begin_src
Input: lists = []
Output: []
#+end_src
*Example 3:*
#+begin_src
Input: lists = [[]]
Output: []
#+end_src
*Constraints:*
- ~k == lists.length~
- ~0 <= k <= 10^{4}~
- ~0 <= lists[i].length <= 500~
- ~-10^{4} <= lists[i][j] <= 10^{4}~
- ~lists[i]~ is sorted in *ascending order*.
- The sum of ~lists[i].length~ will not exceed ~10^{4}~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
#+end_src
** TODO C++
#+begin_src cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
}
};
#+end_src
@@ -1,18 +1,73 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0025. Reverse Nodes In K Group :hard:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0025. Reverse Nodes In K Group][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0025. Reverse Nodes In K Group][0025. Reverse Nodes In K Group]]
:END:
Given the ~head~ of 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 linked list. If the number of nodes is not a multiple of ~k~ then left-out nodes, in the end, should remain as it is.
You may not alter the values in the list's nodes, only nodes themselves may be changed.
*Example 1:*
#+begin_src
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
#+end_src
*Example 2:*
#+begin_src
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
#+end_src
*Constraints:*
- The number of nodes in the list is ~n~.
- ~1 <= k <= n <= 5000~
- ~0 <= Node.val <= 1000~
*Follow-up:* Can you solve the problem in ~O(1)~ extra memory space?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
#+end_src
** TODO C++
#+begin_src cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
}
};
#+end_src
@@ -1,18 +1,102 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0138. Copy List With Random Pointer :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0138. Copy List With Random Pointer][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0138. Copy List With Random Pointer][0138. Copy List With Random Pointer]]
:END:
A linked list of length ~n~ is given such that each node contains an additional random pointer, which could point to any node in the list, or ~null~.
Construct a *deep copy* of the list. The deep copy should consist of exactly ~n~ *brand new* nodes, where each new node has its value set to the value of its corresponding original node. Both the ~next~ and ~random~ pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. *None of the pointers in the new list should point to nodes in the original list*.
For example, if there are two nodes ~X~ and ~Y~ in the original list, where ~X.random --> Y~, then for the corresponding two nodes ~x~ and ~y~ in the copied list, ~x.random --> y~.
Return /the head of the copied linked list/.
The linked list is represented in the input/output as a list of ~n~ nodes. Each node is represented as a pair of ~[val, random_index]~ where:
- ~val~: an integer representing ~Node.val~
- ~random_index~: the index of the node (range from ~0~ to ~n-1~) that the ~random~ pointer points to, or ~null~ if it does not point to any node.
Your code will *only* be given the ~head~ of the original linked list.
*Example 1:*
#+begin_src
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
#+end_src
*Example 2:*
#+begin_src
Input: head = [[1,1],[2,1]]
Output: [[1,1],[2,1]]
#+end_src
*Example 3:*
**
#+begin_src
Input: head = [[3,null],[3,0],[3,null]]
Output: [[3,null],[3,0],[3,null]]
#+end_src
*Constraints:*
- ~0 <= n <= 1000~
- ~-10^{4} <= Node.val <= 10^{4}~
- ~Node.random~ is ~null~ or is pointing to some node in the linked list.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
"""
# Definition for a Node.
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random
"""
class Solution:
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
#+end_src
** TODO C++
#+begin_src cpp
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
*/
class Solution {
public:
Node* copyRandomList(Node* head) {
}
};
#+end_src
@@ -1,18 +1,84 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0141. Linked List Cycle :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0141. Linked List Cycle][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0141. Linked List Cycle][0141. Linked List Cycle]]
:END:
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~.
*Example 1:*
#+begin_src
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).
#+end_src
*Example 2:*
#+begin_src
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.
#+end_src
*Example 3:*
#+begin_src
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
#+end_src
*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.
*Follow up:* Can you solve it using ~O(1)~ (i.e. constant) memory?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
#+end_src
** TODO C++
#+begin_src cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
}
};
#+end_src
@@ -1,18 +1,84 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0143. Reorder List :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0143. Reorder List][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0143. Reorder List][0143. Reorder List]]
:END:
You are given the head of a singly linked-list. The list can be represented as:
#+begin_src
L0 &rarr; L1 &rarr; &hellip; &rarr; Ln - 1 &rarr; Ln
#+end_src
/Reorder the list to be on the following form:/
#+begin_src
L0 &rarr; Ln &rarr; L1 &rarr; Ln - 1 &rarr; L2 &rarr; Ln - 2 &rarr; &hellip;
#+end_src
You may not modify the values in the list's nodes. Only nodes themselves may be changed.
*Example 1:*
#+begin_src
Input: head = [1,2,3,4]
Output: [1,4,2,3]
#+end_src
*Example 2:*
#+begin_src
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
#+end_src
*Constraints:*
- The number of nodes in the list is in the range ~[1, 5 * 10^{4}]~.
- ~1 <= Node.val <= 1000~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
#+end_src
** TODO C++
#+begin_src cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
void reorderList(ListNode* head) {
}
};
#+end_src
@@ -1,18 +1,99 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0146. LRU Cache :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0146. LRU Cache][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0146. LRU Cache][0146. LRU Cache]]
:END:
Design a data structure that follows the constraints of a *Least Recently Used (LRU) cache*.
Implement the ~LRUCache~ class:
- ~LRUCache(int capacity)~ Initialize the LRU cache with *positive* size ~capacity~.
- ~int get(int key)~ Return the value of the ~key~ if the key exists, otherwise return ~-1~.
- ~void put(int key, int value)~ Update the value of the ~key~ if the ~key~ exists. Otherwise, add the ~key-value~ pair to the cache. If the number of keys exceeds the ~capacity~ from this operation, *evict* the least recently used key.
The functions ~get~ and ~put~ must each run in ~O(1)~ average time complexity.
*Example 1:*
#+begin_src
Input
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
Output
[null, null, null, 1, null, -1, null, -1, 3, 4]
Explanation
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // cache is {1=1}
lRUCache.put(2, 2); // cache is {1=1, 2=2}
lRUCache.get(1); // return 1
lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
lRUCache.get(2); // returns -1 (not found)
lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
lRUCache.get(1); // return -1 (not found)
lRUCache.get(3); // return 3
lRUCache.get(4); // return 4
#+end_src
*Constraints:*
- ~1 <= capacity <= 3000~
- ~0 <= key <= 10^{4}~
- ~0 <= value <= 10^{5}~
- At most ~2 * 10^{5}~ calls will be made to ~get~ and ~put~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class LRUCache:
def __init__(self, capacity: int):
def get(self, key: int) -> int:
def put(self, key: int, value: int) -> None:
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
#+end_src
** TODO C++
#+begin_src cpp
class LRUCache {
public:
LRUCache(int capacity) {
}
int get(int key) {
}
void put(int key, int value) {
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/
#+end_src
@@ -1,18 +1,76 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0206. Reverse Linked List :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0206. Reverse Linked List][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0206. Reverse Linked List][0206. Reverse Linked List]]
:END:
Given the ~head~ of a singly linked list, reverse the list, and return /the reversed list/.
*Example 1:*
#+begin_src
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
#+end_src
*Example 2:*
#+begin_src
Input: head = [1,2]
Output: [2,1]
#+end_src
*Example 3:*
#+begin_src
Input: head = []
Output: []
#+end_src
*Constraints:*
- The number of nodes in the list is the range ~[0, 5000]~.
- ~-5000 <= Node.val <= 5000~
*Follow up:* A linked list can be reversed either iteratively or recursively. Could you implement both?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
#+end_src
** TODO C++
#+begin_src cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
}
};
#+end_src
@@ -1,18 +1,73 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0287. Find The Duplicate Number :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0287. Find The Duplicate Number][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0287. Find The Duplicate Number][0287. Find The Duplicate Number]]
:END:
Given an array of integers ~nums~ containing ~n + 1~ integers where each integer is in the range ~[1, n]~ inclusive.
There is only *one repeated number* in ~nums~, return /this repeated number/.
You must solve the problem *without* modifying the array ~nums~ and using only constant extra space.
*Example 1:*
#+begin_src
Input: nums = [1,3,4,2,2]
Output: 2
#+end_src
*Example 2:*
#+begin_src
Input: nums = [3,1,3,4,2]
Output: 3
#+end_src
*Example 3:*
#+begin_src
Input: nums = [3,3,3,3,3]
Output: 3
#+end_src
*Constraints:*
- ~1 <= n <= 10^{5}~
- ~nums.length == n + 1~
- ~1 <= nums[i] <= n~
- All the integers in ~nums~ appear only *once* except for *precisely one integer* which appears *two or more* times.
Follow up:
- How can we prove that at least one duplicate number must exist in ~nums~?
- Can you solve the problem in linear runtime complexity?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int findDuplicate(vector<int>& nums) {
}
};
#+end_src
@@ -1,18 +1,78 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0725. Split Linked List in Parts :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0725. Split Linked List in Parts][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0725. Split Linked List in Parts][0725. Split Linked List in Parts]]
:END:
Given the ~head~ of a singly linked list and an integer ~k~, split the linked list into ~k~ consecutive linked list parts.
The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null.
The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.
Return /an array of the /~k~/ parts/.
*Example 1:*
#+begin_src
Input: head = [1,2,3], k = 5
Output: [[1],[2],[3],[],[]]
Explanation:
The first element output[0] has output[0].val = 1, output[0].next = null.
The last element output[4] is null, but its string representation as a ListNode is [].
#+end_src
*Example 2:*
#+begin_src
Input: head = [1,2,3,4,5,6,7,8,9,10], k = 3
Output: [[1,2,3,4],[5,6,7],[8,9,10]]
Explanation:
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
#+end_src
*Constraints:*
- The number of nodes in the list is in the range ~[0, 1000]~.
- ~0 <= Node.val <= 1000~
- ~1 <= k <= 50~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
#+end_src
** TODO C++
#+begin_src cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
vector<ListNode*> splitListToParts(ListNode* head, int k) {
}
};
#+end_src
@@ -1,18 +1,111 @@
#+PROPERTY: STUDY_DECK_02
* TODO 1472. Design Browser History :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*1472. Design Browser History][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*1472. Design Browser History][1472. Design Browser History]]
:END:
You have a *browser* of one tab where you start on the ~homepage~ and you can visit another ~url~, get back in the history number of ~steps~ or move forward in the history number of ~steps~.
Implement the ~BrowserHistory~ class:
- ~BrowserHistory(string homepage)~ Initializes the object with the ~homepage~ of the browser.
- ~void visit(string url)~ Visits ~url~ from the current page. It clears up all the forward history.
- ~string back(int steps)~ Move ~steps~ back in history. If you can only return ~x~ steps in the history and ~steps > x~, you will return only ~x~ steps. Return the current ~url~ after moving back in history *at most* ~steps~.
- ~string forward(int steps)~ Move ~steps~ forward in history. If you can only forward ~x~ steps in the history and ~steps > x~, you will forward only ~x~ steps. Return the current ~url~ after forwarding in history *at most* ~steps~.
*Example:*
#+begin_src
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:
BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com"
browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com"
browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com"
browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com"
browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com"
browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com"
browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com"
browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps.
browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com"
browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"
#+end_src
*Constraints:*
- ~1 <= homepage.length <= 20~
- ~1 <= url.length <= 20~
- ~1 <= steps <= 100~
- ~homepage~ and ~url~ consist of '.' or lower case English letters.
- At most ~5000~ calls will be made to ~visit~, ~back~, and ~forward~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class BrowserHistory:
def __init__(self, homepage: str):
def visit(self, url: str) -> None:
def back(self, steps: int) -> str:
def forward(self, steps: int) -> str:
# Your BrowserHistory object will be instantiated and called as such:
# obj = BrowserHistory(homepage)
# obj.visit(url)
# param_2 = obj.back(steps)
# param_3 = obj.forward(steps)
#+end_src
** TODO C++
#+begin_src cpp
class BrowserHistory {
public:
BrowserHistory(string homepage) {
}
void visit(string url) {
}
string back(int steps) {
}
string forward(int steps) {
}
};
/**
* Your BrowserHistory object will be instantiated and called as such:
* BrowserHistory* obj = new BrowserHistory(homepage);
* obj->visit(url);
* string param_2 = obj->back(steps);
* string param_3 = obj->forward(steps);
*/
#+end_src
@@ -1,18 +1,69 @@
#+PROPERTY: STUDY_DECK_02
* TODO 1721. Swapping Nodes in a Linked List :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*1721. Swapping Nodes in a Linked List][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*1721. Swapping Nodes in a Linked List][1721. Swapping Nodes in a Linked List]]
:END:
You are given the ~head~ of a linked list, and an integer ~k~.
Return /the head of the linked list after *swapping* the values of the /~k^{th}~ /node from the beginning and the /~k^{th}~ /node from the end (the list is *1-indexed*)./
*Example 1:*
#+begin_src
Input: head = [1,2,3,4,5], k = 2
Output: [1,4,3,2,5]
#+end_src
*Example 2:*
#+begin_src
Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5
Output: [7,9,6,6,8,7,3,0,9,5]
#+end_src
*Constraints:*
- The number of nodes in the list is ~n~.
- ~1 <= k <= n <= 10^{5}~
- ~0 <= Node.val <= 100~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
#+end_src
** TODO C++
#+begin_src cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapNodes(ListNode* head, int k) {
}
};
#+end_src
@@ -1,18 +1,74 @@
#+PROPERTY: STUDY_DECK_02
* TODO 2487. Remove Nodes From Linked List :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*2487. Remove Nodes From Linked List][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*2487. Remove Nodes From Linked List][2487. Remove Nodes From Linked List]]
:END:
You are given the ~head~ of a linked list.
Remove every node which has a node with a greater value anywhere to the right side of it.
Return /the /~head~/ of the modified linked list./
*Example 1:*
#+begin_src
Input: head = [5,2,13,3,8]
Output: [13,8]
Explanation: The nodes that should be removed are 5, 2 and 3.
- Node 13 is to the right of node 5.
- Node 13 is to the right of node 2.
- Node 8 is to the right of node 3.
#+end_src
*Example 2:*
#+begin_src
Input: head = [1,1,1,1]
Output: [1,1,1,1]
Explanation: Every node has value 1, so no nodes are removed.
#+end_src
*Constraints:*
- The number of the nodes in the given list is in the range ~[1, 10^{5}]~.
- ~1 <= Node.val <= 10^{5}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
#+end_src
** TODO C++
#+begin_src cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNodes(ListNode* head) {
}
};
#+end_src