Skip to content

Commit e082962

Browse files
committed
added LinkedList class in ES6 syntax
1 parent 4c18af4 commit e082962

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

chapter05/01-LinkedList2.js

+9-16
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ let LinkedList2 = (function () {
2020
append(element) {
2121

2222
let node = new Node(element),
23-
current,
24-
_head = this.getHead();
23+
current;
2524

26-
if (_head === null) { //first node on list
27-
_head = node;
28-
head.set(this, _head);
25+
if (this.getHead() === null) { //first node on list
26+
head.set(this, node);
2927
} else {
3028

31-
current = _head;
29+
current = this.getHead();
3230

3331
//loop the list until find last item
3432
while (current.next) {
@@ -51,16 +49,14 @@ let LinkedList2 = (function () {
5149
if (position >= 0 && position <= this.size()) {
5250

5351
let node = new Node(element),
54-
_head = this.getHead(),
55-
current = _head,
52+
current = this.getHead(),
5653
previous,
5754
index = 0;
5855

5956
if (position === 0) { //add on first position
6057

6158
node.next = current;
62-
_head = node;
63-
head.set(this, _head);
59+
head.set(this, node);
6460

6561
} else {
6662
while (index++ < position) {
@@ -88,15 +84,13 @@ let LinkedList2 = (function () {
8884
//check for out-of-bounds values
8985
if (position > -1 && position < this.size()) {
9086

91-
let _head = this.getHead(),
92-
current = _head,
87+
let current = this.getHead(),
9388
previous,
9489
index = 0;
9590

9691
//removing first item
9792
if (position === 0) {
98-
_head = current.next;
99-
head.set(this, _head);
93+
head.set(this, current.next);
10094
} else {
10195

10296
while (index++ < position) {
@@ -128,8 +122,7 @@ let LinkedList2 = (function () {
128122

129123
indexOf(element) {
130124

131-
let _head = this.getHead(),
132-
current = _head,
125+
let current = this.getHead(),
133126
index = 0;
134127

135128
while (current) {

0 commit comments

Comments
 (0)