← DiffPush

Intro to linked list

Baseline Linked List · Single Linked List O(n) · O(n)

Chaining Waypoints into a Delivery Route

A routing engine receives a batch of delivery stops and must chain them into a route where each stop knows only the next one — the classic singly-linked structure. The builder walks the array once, wiring each new stop onto the tail of the growing chain. The head of the finished route is handed back to the dispatcher.

Input: An integer n and an array arr of n node values.

Output: The head of the constructed singly linked list containing the values of arr in order.

Constraints

Examples

Example 1

Input: {"n":5,"arr":[1,2,3,4,5]}
Output: [1,2,3,4,5]
Five stops chain in array order; reading the list from the head reproduces the input.

Example 2

Input: {"n":2,"arr":[2,4]}
Output: [2,4]
A two-node chain: head holds 2 and its single link reaches 4.

Solve this in your browser →

Also on LeetCode ↗