File tree Expand file tree Collapse file tree 2 files changed +13
-3
lines changed
src/data-structures/linked-list Expand file tree Collapse file tree 2 files changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -21,7 +21,13 @@ export default class LinkedList {
2121 */
2222 prepend ( value ) {
2323 // Make new node to be a head.
24- this . head = new LinkedListNode ( value , this . head ) ;
24+ const newNode = new LinkedListNode ( value , this . head ) ;
25+ this . head = newNode ;
26+
27+ // If there is no tail yet let's make new node a tail.
28+ if ( ! this . tail ) {
29+ this . tail = newNode ;
30+ }
2531
2632 return this ;
2733 }
Original file line number Diff line number Diff line change @@ -21,10 +21,14 @@ describe('LinkedList', () => {
2121 it ( 'should prepend node to linked list' , ( ) => {
2222 const linkedList = new LinkedList ( ) ;
2323
24- linkedList . append ( 1 ) ;
2524 linkedList . prepend ( 2 ) ;
25+ expect ( linkedList . head . toString ( ) ) . toBe ( '2' ) ;
26+ expect ( linkedList . tail . toString ( ) ) . toBe ( '2' ) ;
27+
28+ linkedList . append ( 1 ) ;
29+ linkedList . prepend ( 3 ) ;
2630
27- expect ( linkedList . toString ( ) ) . toBe ( '2,1' ) ;
31+ expect ( linkedList . toString ( ) ) . toBe ( '3, 2,1' ) ;
2832 } ) ;
2933
3034 it ( 'should delete node by value from linked list' , ( ) => {
You can’t perform that action at this time.
0 commit comments