Problem 202: Happy Number
QuestionLeetCode Link | 202. Happy Number | Easy
Write an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not ...
Problem 242: Valid Anagram
QuestionLeetCode Link | 242. Valid Anagram | Easy
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.
Constrains
1 <= s.length, t.length <= 5 * 10^4
s and t consist of lowercase English letters.
ExamplesExample 1
Input: s = “anagram”, t = “nagaram”Output: true
Example 2
Input: s = “rat”, t ...
Problem 349: Intersection of Two Arrays
QuestionLeetCode Link | 349. Intersection of Two Arrays | Easy
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
Constrains
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
ExamplesExample 1
Input: nums1 = [1,2,2,1], nums2 = [2,2]Output: [2]
Example 2
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]Output: [9,4]Explanation: [4,9] ...
Problem 142: Linked List Cycle II
QuestionLeetCode Link | 142. Linked List Cycle II | Medium
Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.
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 (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.
Do not mod ...
Problem 160: Intersection Of Two Linked Lists
QuestionLeetCode Link | 160. Intersection Of Two Linked Lists | Easy
Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
For example, the following two linked lists begin to intersect at node c1:
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
Note that the linked lists must retain their original structure after the funct ...
Problem 19: Remove Nth Node From End of List
QuestionLeetCode Link | 19. Remove Nth Node From End of List | Medium
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
ExamplesExample 1
Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5]
Example 2
Input: head = [1], n = 1Output: []
Example 3
Input: head = [1,2], n = 1Output: [1]
Solution Appro ...
Problem 24: Swap Nodes in Pairs
QuestionLeetCode Link | 24. Swap Nodes in Pairs | Medium
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
ExamplesExample 1
Input: head = [1,2,3,4]Output: [2,1,4,3]
Example 2
Input: head = []Output: []
Example 3
Input: head = [1]Output: ...
Problem 206: Reverse Linked List
QuestionLeetCode Link | 206. Reverse Linked List | Easy
Given the head of a singly linked list, reverse the list, and return the reversed list.
Constrains
The number of nodes in the list is the range [0, 5000].
-5000 <= Node.val <= 5000
ExampleExample 1
Input: head = [1,2,3,4,5]Output: [5,4,3,2,1]
Example 2
Input: head = [1,2]Output: [2,1]
Example 3
Input: head = []Output: []
Solution ApproachMethod: Two-pointersConstrains
None
This method does not have any limitation ...
Problem 707: Design Linked List
QuestionLeetCode Link | 707. Design Linked List | Medium
Design your implementation of the linked list. You can choose to use a singly or doubly linked list.A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-inde ...
Problem 203: Remove Linked List Elements
QuestionLeetCode Link | 203. Remove Linked List Elements | Easy
Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
Constrains
The number of nodes in the list is in the range [0, 10^4].
1 <= Node.val <= 50
0 <= val <= 50
ExampleExample 1
Input: head = [1,2,6,3,4,5,6], val = 6Output: [1,2,3,4,5]
Example 2
Input: head = [], val = 1Output: []
Example 3
Input: head = [ ...