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

Commit af80c0b

Browse files
adding insertLast method
1 parent 6ce33d6 commit af80c0b

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

‎linkedList/linkedList.js‎

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,40 +42,47 @@ class LinkedList {
4242
if(!node.next) {
4343
return node;
4444
}
45-
4645
node = node.next;
4746
}
47+
}
48+
49+
clear() {
50+
this.head = null;
51+
}
4852

49-
clear() {
50-
this.head = null;
53+
removeFirst() {
54+
if(!this.head) {
55+
return;
5156
}
5257

53-
removeFirst() {
54-
if(!this.head) {
55-
return;
56-
}
58+
this.head = this.head.next;
59+
}
5760

58-
this.head = this.head.next;
61+
removeLast() {
62+
63+
if(!this.head) {
64+
return;
65+
} else if(!this.head.next) {
66+
previous = null;
67+
return;
5968
}
6069

61-
removeLast() {
62-
63-
if(!this.head) {
64-
return;
65-
} else if(!this.head.next) {
66-
previous = null;
67-
return;
68-
}
70+
let previous = this.head;
71+
let node = this.head.next;
6972

70-
let previous = this.head;
71-
let node = this.head.next;
73+
while(node.next) {
74+
previous = node;
75+
node = node.next;
76+
}
7277

73-
while(node.next) {
74-
previous = node;
75-
node = node.next;
76-
}
78+
previous.next = null;
79+
}
7780

78-
previous.next = null;
81+
insertLast(data) {
82+
if(!this.head) {
83+
this.head = new Node(data);
84+
} else {
85+
this.getLast().next = new Node(data);
7986
}
8087
}
8188
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /