EXERCISE · AlgoCasts

Midpoint of a linked list

Return the 'middle' node of a linked list. If the list has an even number of elements, return the node at the end of the first half of the list.

5 testsAlgoCastsGPL-3.0exercise 19 of 26

Directions

Directions

Return the 'middle' node of a linked list. If the list has an even number of elements, return the node at the end of the first half of the list. *Do not* use a counter variable, *do not* retrieve the size of the list, and only iterate through the list one time.

Examples#

JS
const l = new LinkedList();l.insertLast('a')l.insertLast('b')l.insertLast('c')midpoint(l); // returns { data: 'b' }

Try it first#

Copy the stub, make the 5 tests pass on your machine, then open the solution. The reveal is remembered per page in this browser.

JSexercises/midpoint/index.js
function midpoint(list) {} module.exports = midpoint;

See also