...

/

Solution Review: Length of a Linked List

Solution Review: Length of a Linked List

This review provides a detailed analysis of the solution to find the length of a linked list.

We'll cover the following...

Solution: Using Recursion

Press + to interact
index.js
node.js
linkedList.js
import LinkedList from './linkedList.js'
function length(testVariable, head) {
// Base case
if (head == null) {
return 0;
}
// Recursive case
else {
return 1 + length(testVariable, head.next);
}
}
// Driver Code
var list = new LinkedList();
list.appendNode(4);
list.appendNode(3);
list.appendNode(11);
list.appendNode(7);
console.log(length(list, list.head))

Explanation

Have a look at the function length() in index.js file.

The base case (line number 5 to 7) for this problem is when a linked list contains no nodes. In this case, we ...

Access this course and 1400+ top-rated courses and projects.