@@ -78,7 +78,7 @@ class CircularDoublyLinkedList {
7878 */
7979 const newNode = new CircularDoublyLinkedListNode ( data ) ;
8080
81- //special case: no items in the list yet
81+ // special case: no items in the list yet
8282 if ( this [ head ] === null ) {
8383
8484 /*
@@ -413,13 +413,8 @@ class CircularDoublyLinkedList {
413413 */
414414 remove ( index ) {
415415
416- // special case: no nodes in the list
417- if ( this [ head ] === null ) {
418- throw new RangeError ( `Index ${ index } does not exist in the list.` ) ;
419- }
420-
421- // special case: `index` is an invalid value
422- if ( index < 0 ) {
416+ // special cases: no nodes in the list or `index` is an invalid value
417+ if ( ( this [ head ] === null ) || ( index < 0 ) ) {
423418 throw new RangeError ( `Index ${ index } does not exist in the list.` ) ;
424419 }
425420
@@ -456,13 +451,6 @@ class CircularDoublyLinkedList {
456451 return current . data ;
457452 }
458453
459- /*
460- * The `previous` variable keeps track of the node just before
461- * `current` in the loop below. This is necessary because removing
462- * an node means updating the previous node's `next` pointer.
463- */
464- let previous = null ;
465-
466454 /*
467455 * The `i` variable is used to track how deep into the list we've
468456 * gone. This is important because it's the only way to know when
@@ -478,9 +466,6 @@ class CircularDoublyLinkedList {
478466 */
479467 do {
480468
481- // save the value of current
482- previous = current ;
483-
484469 // traverse to the next node
485470 current = current . next ;
486471
@@ -496,8 +481,8 @@ class CircularDoublyLinkedList {
496481 if ( current !== this [ head ] ) {
497482
498483 // skip over the node to remove
499- previous . next = current . next ;
500- current . next . previous = previous ;
484+ current . previous . next = current . next ;
485+ current . next . previous = current . previous ;
501486
502487 // return the value that was just removed from the list
503488 return current . data ;
0 commit comments