File tree Expand file tree Collapse file tree 2 files changed +11
-13
lines changed
lib/data-structures/linked-lists Expand file tree Collapse file tree 2 files changed +11
-13
lines changed Original file line number Diff line number Diff line change @@ -36,23 +36,19 @@ class LinkedList {
3636
3737 removeLast ( ) { // similar to Array.pop
3838 const current = this . root ;
39+ let target ;
3940
40- if ( ! current || ! current . next ) {
41- const target = current ;
42- this . root = null ;
43- if ( target ) {
44- return target . value ;
41+ if ( current && current . next ) {
42+ while ( current && current . next && current . next . next ) {
43+ current = current . next ;
4544 }
46- return ;
47- }
48-
49- while ( current && current . next && current . next . next ) {
50- current = current . next ;
45+ target = current . next ;
46+ current . next = null ;
47+ } else {
48+ this . root = null ;
49+ target = current ;
5150 }
5251
53- const target = current . next ;
54- current . next = null ;
55-
5652 if ( target ) {
5753 return target . value ;
5854 }
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ describe('LinkedList', function () {
3535
3636 expect ( linkedList . removeFirst ( ) ) . toBe ( 'b' ) ;
3737 expect ( linkedList . removeFirst ( ) ) . toBe ( undefined ) ;
38+ expect ( linkedList . removeFirst ( ) ) . toBe ( undefined ) ;
3839 } ) ;
3940 } ) ;
4041
@@ -65,6 +66,7 @@ describe('LinkedList', function () {
6566
6667 expect ( linkedList . removeLast ( ) ) . toBe ( 'a' ) ;
6768 expect ( linkedList . removeLast ( ) ) . toBe ( undefined ) ;
69+ expect ( linkedList . removeLast ( ) ) . toBe ( undefined ) ;
6870 } ) ;
6971 } ) ;
7072} ) ;
You can’t perform that action at this time.
0 commit comments