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 eea8c32

Browse files
refactoring methods using available method
1 parent 7f0d69b commit eea8c32

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

‎linkedList/linkedList.js‎

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class LinkedList {
1515
}
1616

1717
insertFirst(data) {
18-
this.head = new Node(data, this.head);
18+
// this.head = new Node(data, this.head);
19+
this.insertAt(data,0);
1920
}
2021

2122
size() {
@@ -29,61 +30,68 @@ class LinkedList {
2930
}
3031

3132
getFirst() {
32-
return this.head;
33+
// return this.head;
34+
return this.getAt(0);
3335
}
3436

3537
getLast() {
36-
if(!this.head) {
37-
return null;
38-
}
39-
40-
let node = this.head;
41-
while(node) {
42-
if(!node.next) {
43-
return node;
44-
}
45-
node = node.next;
46-
}
38+
// if(!this.head) {
39+
// return null;
40+
// }
41+
42+
// let node = this.head;
43+
// while(node) {
44+
// if(!node.next) {
45+
// return node;
46+
// }
47+
// node = node.next;
48+
// }
49+
50+
return this.getAt(this.size()-1);
4751
}
4852

4953
clear() {
5054
this.head = null;
5155
}
5256

5357
removeFirst() {
54-
if(!this.head) {
55-
return;
56-
}
58+
// if(!this.head) {
59+
// return;
60+
// }
5761

58-
this.head = this.head.next;
62+
// this.head = this.head.next;
63+
this.removeAt(0);
5964
}
6065

6166
removeLast() {
6267

63-
if(!this.head) {
64-
return;
65-
} else if(!this.head.next) {
66-
previous = null;
67-
return;
68-
}
68+
// if(!this.head) {
69+
// return;
70+
// } else if(!this.head.next) {
71+
// previous = null;
72+
// return;
73+
// }
6974

70-
let previous = this.head;
71-
let node = this.head.next;
75+
// let previous = this.head;
76+
// let node = this.head.next;
7277

73-
while(node.next) {
74-
previous = node;
75-
node = node.next;
76-
}
78+
// while(node.next) {
79+
// previous = node;
80+
// node = node.next;
81+
// }
82+
83+
// previous.next = null;
7784

78-
previous.next=null;
85+
this.removeAt(this.size()-1);
7986
}
8087

8188
insertLast(data) {
82-
if(!this.head) {
83-
this.head = new Node(data);
84-
} else {
85-
this.getLast().next = new Node(data);
86-
}
89+
// if(!this.head) {
90+
// this.head = new Node(data);
91+
// } else {
92+
// this.getLast().next = new Node(data);
93+
// }
94+
this.insertAt(data, this.size()-1);
8795
}
8896

8997
getAt(index) {

0 commit comments

Comments
(0)

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