medium Data Structure

Merge Sorted Lists

Write a function mergeSortedLists that takes two sorted linked lists and merges them into one sorted list.

Function Signature

function mergeSortedLists(l1, l2);

Example 1:

Input: l1 = [1, 2, 4], l2 = [1, 3, 4]
Output: [1, 1, 2, 3, 4, 4]
Explanation: The merged list is sorted.

Example 2:

Input: l1 = [], l2 = []
Output: []
Explanation: Merging two empty lists results in an empty list.

Example 3:

Input: l1 = [], l2 = [0]
Output: [0]
Explanation: Merging an empty list with a non-empty list results in the non-empty list.

Solve this problem

Start your 7-day free trial to solve this problem in our code editor with instant feedback.

Start 7-Day Free Trial to Solve This Problem

Related Problems

Merge Intervals

medium

Write a function `mergeIntervals` that takes an array of intervals and merges all overlapping intervals. ## Function Signature ```function mergeIntervals(intervals);``` ### Example 1: > **Input:** `intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]` \ > **Output:** `[[1, 6], [8, 10], [15, 18]]` \ > **Explanation:** Since intervals [1, 3] and [2, 6] overlap, merge them into [1, 6]. ### Example 2: > **Input:** `intervals = [[1, 4], [4, 5]]` \ > **Output:** `[[1, 5]]` \ > **Explanation:** Intervals [1, 4] and [4, 5] are considered overlapping.

Array

Best Time to Buy and Sell Stock

medium

Write a function `maxProfit` that takes an array where the `i-th` element is the price of a given stock on day `i`, and returns the maximum profit you can achieve by buying and selling one share of the stock. ## Function Signature ```function maxProfit(prices);``` ### Example 1: > **Input:** `prices = [7,1,5,3,6,4]` \ > **Output:** `5` \ > **Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. ### Example 2: > **Input:** `prices = [7,6,4,3,1]` \ > **Output:** `0` \ > **Explanation:** In this case, no transactions are done, i.e. max profit = 0.

Array

Group Anagrams

medium

Write a function `groupAnagrams` that takes an array of strings and groups the anagrams together. ## Function Signature ```function groupAnagrams(strs);``` ### Example 1: > **Input:** `strs = ["eat", "tea", "tan", "ate", "nat", "bat"]` \ > **Output:** `[["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]` \ > **Explanation:** All strings that are anagrams are grouped together. ### Example 2: > **Input:** `strs = [""]` \ > **Output:** `[[""]]` \ > **Explanation:** An empty string is considered an anagram of itself. ### Example 3: > **Input:** `strs = ["a"]` \ > **Output:** `[["a"]]` \ > **Explanation:** A single character string is considered an anagram of itself.

String