Challenge: Inserting Elements in a Linked List
Test your knowledge by solving this coding challenge.
Introduction
To practice working with linked lists, you’ll have to implement the following functions:
listLength
, which returns the number of elements inside a linked list.insertAtPos
, which inserts an element at a specified position inside a list.
The listLength
function
Problem statement
Write a function listLength
, which takes a list as input and returns the number of elements inside the list.
For example:
- For the list
[1, 2, 3, 4, 5]
, return5
, which is the number of elements. - For the empty list
[]
, return0
.
Challenge
Complete the function in the following code widget. The definition of the list structure is already present in the background.
Use a single loop.
Get hands-on with 1400+ tech skills courses.