Given two lists of integers, return a new list created by alternating elements from each list. Start with the first element of list1, then first element of list2, then second of list1, and so on. If one list is longer, append the remaining elements at the end. Return an empty list if both inputs are null. If one is null, return the other.

Examples:

Input: list1 = [1, 3, 5], list2 = [2, 4, 6] Output: [1, 2, 3, 4, 5, 6] Explanation: Alternates between lists - 1 from list1, 2 from list2, 3 from list1, 4 from list2, 5 from list1, 6 from list2 Input: list1 = [1, 2], list2 = [3, 4, 5, 6] Output: [1, 3, 2, 4, 5, 6] Explanation: After exhausting list1, remaining elements from list2 are appended Input: list1 = [10, 20, 30], list2 = [40] Output: [10, 40, 20, 30] Explanation: After list2 is exhausted, remaining elements from list1 are appended Input: list1 = null, list2 = [1, 2] Output: [1, 2] Explanation: When one list is null, return the other list Input: list1 = null, list2 = null Output: [] Explanation: When both lists are null, return empty list
Apex Code Editor

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay Mar 2026

Contest runs March 3 - 31. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.