data-structure-algebra/doubly-linked-list

View on GitHub
src/_last.js

Summary

Maintainability
A
0 mins
Test Coverage
import assert from 'assert';

import Node from './Node.js';

/**
 * Returns the last node of a list. The list cannot be empty.
 *
 * @param {Node} first First node of the list.
 * @return {Node} Last node of the list.
 */
export default function _last(first) {
    assert(first instanceof Node);
    let next = first;

    while (next.next !== null) {
        next = next.next;
    }

    return next;
}