data-structure-algebra/circularly-linked-list

View on GitHub
src/_rotate_right.js

Summary

Maintainability
A
1 hr
Test Coverage
import assert from 'assert';

import Node from './Node.js';

/**
 * Rotate list to the right n steps. The parameter n must be positive.
 *
 * @param {Node} x The current first node.
 * @param {number} n MUST be positive.
 * @return {Node} The new first node.
 */
export default function _rotate_right(x, n) {
    assert(Number.isInteger(n));
    assert(n > 0);
    assert(x instanceof Node);
    do {
        x = x.next;
    } while (--n);

    return x;
}