2022-05-24

Description

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists in a 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]

Example 2:

Input: list1 = [], list2 = []
Output: []

Example 3:

Input: list1 = [], list2 = [0]
Output: [0]

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.

Solution

Approach #0

Approach #1: Recursive

Description

Given the head of a singly linked list, reverse the list, and return the reversed list.

Example 1:

Example 2:

Example 3:

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?

Solution

Approach #0: Iterative

Approach #1: Recursive

Description

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

Return the running sum of nums.

Example 1:

Example 2:

Example 3:

Constraints:

  • 1 <= nums.length <= 1000

  • -10^6 <= nums[i] <= 10^6

Solution

Approach #0

Description

Given two strings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false otherwise.

Each letter in magazine can only be used once in ransomNote.

Example 1:

Example 2:

Example 3:

Constraints:

  • 1 <= ransomNote.length, magazine.length <= 10^5

  • ransomNote and magazine consist of lowercase English letters.

Solution

Approach #0

Description

A binary tree is uni-valued if every node in the tree has the same value.

Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.

Example 1:

Example 2:

Constraints:

  • The number of nodes in the tree is in the range [1, 100].

  • 0 <= Node.val < 100

Solution

Approach #0: BFS

Approach #1: DFS

Last updated