Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbeb8501

Browse files
m-maksyutintrekhleb
authored andcommitted
Fix the prepend method for the LinkedList (trekhleb#47)
* Fix LinkedList* Fix the prepend method for the LinkedList
1 parent91d4714 commitbeb8501

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

‎src/data-structures/linked-list/LinkedList.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff 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=newLinkedListNode(value,this.head);
24+
constnewNode=newLinkedListNode(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
returnthis;
2733
}

‎src/data-structures/linked-list/__test__/LinkedList.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ describe('LinkedList', () => {
2121
it('should prepend node to linked list',()=>{
2222
constlinkedList=newLinkedList();
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',()=>{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp