Challenge: Inserting Elements in a Linked List


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], return 5, which is the number of elements.
  • For the empty list [], return 0.


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 1200+ tech skills courses.