Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Print list elements

Traverse a singly linked list and collect its elements in order.

Python practice6 minLinked ListsBeginnerLast updated March 27, 2026

Problem statement

Given the head of a singly linked list, return a Python list containing the values of the nodes in order. If the list is empty (head is None), return an empty list. You must implement traversal yourself by following next pointers; do not convert using external libraries.

Task

Implement traversal of a singly linked list and return a list of node values in order from head to tail.

Examples

Basic traversal

Input

head = build_linked_list([1, 2, 3]) print_list(head)

Output

[1, 2, 3]

Visit nodes from head to tail and collect 1, then 2, then 3.

Input format

A single argument: head (ListNode or None) representing the head of a singly linked list.

Output format

A Python list of integers representing node values in traversal order.

Constraints

- Node values are integers. - List length can be 0 to 10^5 (solutions must traverse only once).

Samples

Sample 1

Input

build_linked_list([5])

Output

[5]

Single-node list returns a list with that one value.