LEETCODE
169. Majority Element Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. Example: Input: nums = [2,2,1,1,1,2,2] Output: 2
Algorithm (Moore's Voting Algorithm): - assume the first element is the majority element - keep a counter, starting at 0 - iterate the array: - if counter = 0, set the majority element to the current element - if current = majority, counter++ - if current != majority, counter-- - return majority - this works because the majority element will always come out on top Time: O(N) Space: O(1)
271. Encode and Decode Strings Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings. Machine 1 (sender) has the function: string encode(vector<string> strs) { // ... your code return encoded_string; } Machine 2 (receiver) has the function: vector<string> decode(string s) { //... your code return strs; } So Machine 1 does: string encoded_string = encode(strs); and Machine 2 does: vector<string> strs2 = decode(encoded_string); strs2 in Machine 2 should be the same as strs in Machine 1. Implement the encode and decode methods. You are not allowed to solve the problem using any serialize methods (such as eval). Example: Input: dummy_input = ["Hello","World"] Output: ["Hello","World"] Explanation: Machine 1: Codec encoder = new Codec(); String msg = encoder.encode(strs); Machine 1 ---msg---> Machine 2 Machine 2: Codec decoder = new Codec(); String[] strs = decoder.decode(msg);
Algorithm: - Encode: input each element as {len(string)}:{string} - Decode: use start and end indices to extract each string Topics: - string - array - neetcode arrays & hashing
704. Binary Search Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity. Example: Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4
Algorithm: - binary search algorithm: keep track of a search space using start, end, and middle. Keep comparing target to the middle element and keep halving the search space. Topics: - neetcode binary search
21. Merge Two Sorted Lists 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: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4]
Algorithm: - define a dummy head (and curr node to append to merged list) - iterate the two lists, appending the next smallest node and eventually the remaining of the lists - return dummyhead.next Topics: - linked list - dummy head
703. Kth Largest Element In A Stream Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element. Implement KthLargest class: KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums. int add(int val) Appends the integer val to the stream and returns the element representing the kth largest element in the stream.
Algorithm: - define a heap from nums using heapq.heapify() - make sure the min-heap only has k elements -- this way, the first element will always be the Kth largest value Topics: - neetcode heap / priority queue
1046. Last Stone Weight You are given an array of integers stones where stones[i] is the weight of the ith stone. We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights x and y with x <= y. The result of this smash is: If x == y, both stones are destroyed, and If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x. At the end of the game, there is at most one stone left. Return the weight of the last remaining stone. If there are no stones left, return 0. Example: Input: stones = [2,7,4,1,8,1] Output: 1 Explanation: We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then, we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then, we combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
Algorithm: - define a max heap from stones array - while len(heap) > 1, pop the 2 largest and perform corresponding rule Topics: - max heap - neetcode heap / priority queue
74. Search a 2D Matrix You are given an m x n integer matrix matrix with the following two properties: Each row is sorted in non-decreasing order. The first integer of each row is greater than the last integer of the previous row. Given an integer target, return true if target is in matrix or false otherwise. You must write a solution in O(log(m * n)) time complexity. Example: Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 Output: true
Algorithm: - each matrix index (0 to m*n - 1) can be represented as matrix[matrixIndex / n][matrixIndex % n] - perform binary search Topics: - matrix - neetcode binary search
31. Next Permutation The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order). Example: Input: nums = [1,2,3] Output: [1,3,2]
Algorithm: - find the right-most (lowest weight) local maximum - special case: if the array is strictly decreasing, swap to sort the whole array - special case: if there is a RHS value less than peak and greater than LHS value to swap, replace RHS value as new peak - swap peak with LHS value - sort everything to the right of new peak location Time: O(N log N) Space: O(1)
54. Spiral Matrix Given an m x n matrix, return all elements of the matrix in spiral order. Example: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5]
Algorithm: - have constant int[][] DIRS to keep track of directions - have constant int VISITED to keep track of which cells have been visited - iterate over the array, switching directions when out of bounds, and taking into consideration the visited squares as a new boundary Topics: - matrix Time: O(m*n) Space: O(1)
347. Top K Frequent Elements Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. Example: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2]
Algorithm: - iterate nums and populate a hashmap with num: occurrences - use a freqeuncy array where each index represents frequencies - iterate frequency array from the back to get k most frequent elements Topics: - hashmap - array - neetcode arrays & hashing
217. Contains Duplicate Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. Example: Input: nums = [1,2,3,1] Output: true
Algorithm: - iterate nums and store in hashset - return true if duplicate is found, else return false Topics: - hashset - neetcode arrays & hashing
20. Valid Parentheses Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type. Example: Input: s = "()" Output: true
Algorithm: - iterate the string: (1) if opening bracket, push to stack (2) if closing bracket, it should match stack.pop() Topics: - stack - neetcode stack
121. Best Time to Buy and Sell Stock You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Example: Input: prices = [7,1,5,3,6,4] Output: 5
Algorithm: - iterate through the array - keep updating minVal - keep updating maxProfit (prices[i] - minVal) - return maxProfit Topics: - arrays - dynamic programming Time: O(N) Space: O(1)
80. Remove Duplicates from Sorted Array II Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements. Example: Input: nums = [1,1,1,2,2,3] Output: 5, nums = [1,1,2,2,3,_]
Algorithm: - p1 tracks where the next qualifying value goes - p2 tracks the current value - use a "nested" while loop to determine how many instances of p2 value there are Time: O(N) Space: O(1)
125. Valid Palindrome A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise. Example: Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome.
Algorithm: - remove all uppercase and non-alphanumeric characters - p1 starts and front, p2 at end. Compare until convergence Topics: - string - neetcode two pointer
189. Rotate Array Given an integer array nums, rotate the array to the right by k steps, where k is non-negative. Example: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]
Algorithm: - reverse entire array - reverse first k elements - reverse last n-k elements Time: O(N) Space: O(1)
0056. Merge Intervals Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input. Example: Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]]
Algorithm: - sort intervals based on the start values - create a linked list to store the merged intervals (because it's easy to access and modify the last end value) - iterate intervals while merging when needed Topics: - array - sorting -linked list - Palantir prep
55. Jump Game You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your *maximum* jump length at that position. Return true if you can reach the last index, or false otherwise. Example: Input: nums = [2,3,1,1,4] Output: true
Algorithm: - start from the back & hopscotch your way to the front - if you can reach the first element, return true Topics: - arrays - greedy - dynamic programming Time: O(N) Space: O(1)
128. Longest Consecutive Sequence Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. You must write an algorithm that runs in O(n) time. Example: Input: nums = [100,4,200,1,3,2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Algorithm: - store nums as set - iterate nums. if num-1 not in set, start seeing getting the current streak, keeping track of maximum streak - this utilizes hashset's O(1) lookup time Topics: - set - neetcode arrays & hashing
210. Course Schedule II There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array. Example: Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]] Output: [0,2,1,3]
Algorithm: - store the classes as directed graph in adjacency list (defaultdict) - keep track of visit status to see if cycles exist - dfs function to recursively add "leaf" nodes to stack - return stack Topics: - DFS - recursion - graph - stack
155. Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class: MinStack() initializes the stack object. void push(int val) pushes the element val onto the stack. void pop() removes the element on the top of the stack. int top() gets the top element of the stack. int getMin() retrieves the minimum element in the stack. You must implement a solution with O(1) time complexity for each function.
Algorithm: - use 2 stacks: stack and min_stack - min_stack mirrors stack and min_stack[i] represents the current minimum value for stack[0:i] Topics: - neetcode stack
97. Interleaving String Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2. Example: Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" Output: true
Algorithm: - use a 2DP boolean matrix of s1.length+1 * s2.length+1 - populate the matrix. Each cell represents if those substrings could make the rest of the s3 substring (p3 = p1+p2) - return matrix[0][0] Topics: - dynamic programming - caching Time: O(m*n) Space: O(m*n)
718. Maximum Length of Repeated Subarray Given two integer arrays nums1 and nums2, return the maximum length of a subarray that appears in both arrays. Example: Input: nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum length is [3,2,1].
Algorithm: - use a dynamic programming matrix to keep track of maximum subarray length at a given point in nums1 and nums2 Topics: - dynamic programming
36. Valid Sudoku Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition. Note: A Sudoku board (partially filled) could be valid but is not necessarily solvable. Only the filled cells need to be validated according to the mentioned rules. Example: Input: board = [["5","3",".",".","7",".",".",".","."] ,["6",".",".","1","9","5",".",".","."] ,[".","9","8",".",".",".",".","6","."] ,["8",".",".",".","6",".",".",".","3"] ,["4",".",".","8",".","3",".",".","1"] ,["7",".",".",".","2",".",".",".","6"] ,[".","6",".",".",".",".","2","8","."] ,[".",".",".","4","1","9",".",".","5"] ,[".",".",".",".","8",".",".","7","9"]] Output: true
Algorithm: - use a list of sets for each row, column, and box - iterate through the matrix, populating and checking against each set Topics: - matrix - array - math trick - neetcode arrays & hashing
3. Longest Substring Without Repeating Characters Given a string s, find the length of the longest substring without repeating characters. Example: Input: "abcabcbb" Output: 3; "abc"
Algorithm: - use a sliding window and a hashset - once you see duplicates, decrease the window through the left until there are no more duplicates, removing from the hashset in the process - keep track of current maximum substring Time: O(N) Space: O(N)
424. Longest Repeating Character Replacement You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times. Return the length of the longest substring containing the same letter you can get after performing the above operations. Example: Input: s = "ABAB", k = 2 Output: 4 Explanation: Replace the two 'A's with two 'B's or vice versa.
Algorithm: - use a sliding window to find maximum substring. A valid substring is lengthOfWindow - currentMaxFreqCharCount <= k - track currentMaxFreqCharCount using hashmap (O(26)) for character: frequency in current window - window expands from start until no longer valid & winnows down by moving left pointer to the right Topics: - map - neetcode sliding window
1472. Design Browser History 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.
Algorithm: - use an ArrayList (dynamic array) to keep track of current url paths. Overwrite new paths instead of deleting by keeping track of the last url Topics: - arraylist - Roblox OA practice
1419. Minimum Number of Frogs Croaking You are given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple "croak" are mixed. Return the minimum number of different frogs to finish all the croaks in the given string. A valid "croak" means a frog is printing five letters 'c', 'r', 'o', 'a', and 'k' sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of a valid "croak" return -1. Example: Input: croakOfFrogs = "crcoakroak" Output: 2
Algorithm: - use an array to represent counts for each character in croak - since we know croak must be sequential, we can "keep track" (increment and decrement) of where the frog is in its croak Topics: - string - counting - Roblox OA practice Time: O(N) Space: O(1)
2225. Find Players With Zero or One Players You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match. Return a list answer of size 2 where: answer[0] is a list of all players that have not lost any matches. answer[1] is a list of all players that have lost exactly one match. The values in the two lists should be returned in increasing order. Example: Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]] Output: [[1,2,10],[4,5,7,8]]
Algorithm: - use array of size 10^5 + 1 to store number of losses where each index represents the player Topics: - counting sort - array
238. Product of Array Except Self Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation. Example: Input: nums = [1,2,3,4] Output: [24,12,8,6]
Algorithm: - use dynamic programming to keep track of left prefix (in result array) and right prefix products (on the fly as a constant) - the result[i] will be left prefix * right prefix Topics: - dynamic programming - array - neetcode arrays & hashing
0811. Subdomain Visit Count A website domain "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com" and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly. A count-paired domain is a domain that has one of the two formats "rep d1.d2.d3" or "rep d1.d2" where rep is the number of visits to the domain and d1.d2.d3 is the domain itself. For example, "9001 discuss.leetcode.com" is a count-paired domain that indicates that discuss.leetcode.com was visited 9001 times. Given an array of count-paired domains cpdomains, return an array of the count-paired domains of each subdomain in the input. You may return the answer in any order. Example: Input: cpdomains = ["9001 discuss.leetcode.com"] Output: ["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"]
Algorithm: - use hashmap to store subdomain : visits - iterate through cpdomains and each subdomain within, adding to the hashmap as you go - iterate the hashmap to create list to return Topics: - hashmap - string - Palantir prep
88. Merge Sorted Array You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n. Example: Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]
Algorithm: - use p1 and p2 to track the largest numbers from nums1 and nums - use p3 to track where in nums1 the next largest number should go Time: O(m+n) Space: O(1)
27. Remove Element Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val. Example: Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2,_,_]
Algorithm: - use two pointer method: p1 tracks next non-val element spot; p2 tracks current - when p2.val != val, fill in and increment Time: O(N) Space: O(1)
26. Remove Duplicates from Sorted Array Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums. Example: Input: nums = [1,1,2] Output: 2, nums = [1,2,_]
Algorithm: - use two pointer method: p1 tracks next unique element spot; p2 tracks current - since array is sorted, when p2.val > p1.val, we can fill in and increment Time: O(N) Space: O(1)
509. Fibonacci Number The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. Example: Input: n = 3 Output: 2
Algorithm: - define the base cases: fib(0)=0, fib(1)=1 - use recursion / memoization (top-down) to calculate the rest! Topics: - recursion - dynamic programming - memoization Time: O(N) Space: O(1)
242. Valid Anagram Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Example: Input: s = "anagram", t = "nagaram" Output: true
Algorithm: - since the strings only consist of lowercase English letters, we can define a length 26 array to keep count of letter occurrences (python offset is 97) - if the strings could contain any unicode character, we could use a hashmap (letter: occurrence) and then just compare the hashmaps for each string Topics: - array - ascii - hashmap - neetcode arrays & hashing
15. 3Sum Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets. Example: Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]]
Algorithm: - sort array - fix i and perform two sum on the remaining subarray using a two pointer method, since the array is sorted - skip duplicates by not allowing duplicate nums[i] values Time: O(N^2) Space: O(N^2), from returned list
1. Two Sum Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Algorithm: create a HashMap to store 'value:index'. Iterate through the array, checking if target-currValue exists in the Map. Time: O(N) Space: O(N)
11. Container With Most Water You are given an integer array height of length n that repreesent n vertical lines. Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. Example: Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water the container contains is 7*7 (width * height)
Algorithm: initialize two pointers at each end. Keep moving the pointer for the lower height towards the center, updating the maximum volume along the way Time: O(N) Space: O(1)
682. Baseball Game You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record. You are given a list of strings operations, where operations[i] is the ith operation you must apply to the record and is one of the following: - An integer x.Record a new score of x. - '+'.Record a new score that is the sum of the previous two scores. - 'D'.Record a new score that is the double of the previous score. - 'C'.Invalidate the previous score, removing it from the record. Return the sum of all the scores on the record after applying all the operations. The test cases are generated such that the answer and all intermediate calculations fit in a 32-bit integer and that all operations are valid. Example 1: Input: ops = ["5","2","C","D","+"] Output: 30 Explanation: "5" - Add 5 to the record, record is now [5]. "2" - Add 2 to the record, record is now [5, 2]. "C" - Invalidate and remove the previous score, record is now [5]. "D" - Add 2 * 5 = 10 to the record, record is now [5, 10]. "+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15]. The total sum is 5 + 10 + 15 = 30.
Algorithm: stack & match/case statement Topics: - stack - match/case statement
1929. Concatenation of Array Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed). Specifically, ans is the concatenation of two nums arrays. Return the array ans. Example 1: Input: nums = [1,2,1] Output: [1,2,1,1,2,1] Explanation: The array ans is formed as follows: - ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]] - ans = [1,2,1,1,2,1]
Algorithm: three pointers - p1 keeps track of ans index for first insertion of nums - p2 keeps track of ans index for second insertion of nums - p3 iterates nums Topics: - array - "two" pointers
206. Reverse Linked List Given the head of a singly linked list, reverse the list, and return the reversed list. Example 1: Input: head = [1,2,3,4,5] Output: [5,4,3,2,1]
Algorithm: two pointer technique + temp ListNode - `prev` starts as None and reps the will-be next node - `curr` starts as head and is the node for which we are reversing the next node - temp keeps a pointer on the next curr node - return prev Topics: - linked list - two pointers