Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Build a singly linked listE02. Append and prepend nodesE03. Compute list lengthE

Problem No 3

Compute list length

Easy

10 minute session

Summary

Traverse a singly linked list to count its nodes.

Problem statement

Given a Python list of values, build a singly linked list and compute its length by traversing nodes. Implement list_length(values) that returns the integer count. The function must work for empty lists and should traverse the linked list node by node.

Task

Implement a function that returns the number of nodes in a linked list built from a Python list.

Examples

Count three elements

Input

[1, 2, 3]

Output

3

Explanation

There are three nodes in the constructed list.

Input format

A Python list of values.

Output format

An integer representing the number of nodes.

Constraints

Time complexity O(n), extra space O(1) apart from the list nodes.

Samples

Sample input 0

[]

Sample output 0

0

Explanation 0

Empty input produces an empty list with length 0.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

03:44 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.