Skip to content
Onsite

Fill Gaps in Linked List

MediumP3Coding

You are given a sorted linked list of unique integers (ascending). Insert new nodes between existing ones so that every pair of adjacent values differs by exactly 1. You may only insert nodes between existing nodes; do not change or remove existing values. Return the head. A list node has val and next. The ListNode class is already defined for you.

Example 1

Input
head = [1,4,6]
Output
[1,2,3,4,5,6]

Example 2

Input
head = [3,4,5]
Output
[3,4,5]

Example 3

Input
head = [7]
Output
[7]

Constraints

  • 0 <= number of nodes <= 1000
  • The values are strictly increasing
  • -10^4 <= Node.val <= 10^4 and the largest value minus the smallest is at most 10^4

Hints

Run checks the 3 visible tests, Submit all 11.