mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 15:53:02 +08:00
Merge 127ee76b37
into ca3d16dcce
This commit is contained in:
commit
3d88c4923a
@ -5,12 +5,14 @@
|
|||||||
"env": {
|
"env": {
|
||||||
"jest/globals": true
|
"jest/globals": true
|
||||||
},
|
},
|
||||||
|
"ignorePatterns": ["*.md", "*.png", "*.jpeg", "*.jpg"],
|
||||||
"rules": {
|
"rules": {
|
||||||
"no-bitwise": "off",
|
"no-bitwise": "off",
|
||||||
"no-lonely-if": "off",
|
"no-lonely-if": "off",
|
||||||
"class-methods-use-this": "off",
|
"class-methods-use-this": "off",
|
||||||
"arrow-body-style": "off",
|
"arrow-body-style": "off",
|
||||||
"no-loop-func": "off"
|
"no-loop-func": "off",
|
||||||
|
"linebreak-style": ["error", "windows"]
|
||||||
},
|
},
|
||||||
"ignorePatterns": ["*.md", "*.png", "*.jpeg", "*.jpg"],
|
"ignorePatterns": ["*.md", "*.png", "*.jpeg", "*.jpg"],
|
||||||
"settings": {
|
"settings": {
|
||||||
|
@ -63,27 +63,32 @@ export default class LinkedList {
|
|||||||
const index = rawIndex < 0 ? 0 : rawIndex;
|
const index = rawIndex < 0 ? 0 : rawIndex;
|
||||||
if (index === 0) {
|
if (index === 0) {
|
||||||
this.prepend(value);
|
this.prepend(value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
// if we have single node in linked list and we have to insert a value we can insert at last
|
||||||
|
if (this.head === this.tail) {
|
||||||
|
this.append(value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
const newNode = new LinkedListNode(value);
|
||||||
|
let count = 1;
|
||||||
|
let currentNode = this.head;
|
||||||
|
|
||||||
|
while (currentNode) {
|
||||||
|
if (count === index) break;
|
||||||
|
currentNode = currentNode.next;
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
// if there is a node in the position we want to insert
|
||||||
|
if (currentNode) {
|
||||||
|
newNode.next = currentNode.next;
|
||||||
|
currentNode.next = newNode;
|
||||||
} else {
|
} else {
|
||||||
let count = 1;
|
/* if index exceeds the size of linked list we
|
||||||
let currentNode = this.head;
|
will be appending the node at last or if the linked
|
||||||
const newNode = new LinkedListNode(value);
|
list dosent exist we will be appending the value that
|
||||||
while (currentNode) {
|
means inserting a new value into the linked list */
|
||||||
if (count === index) break;
|
this.append(value);
|
||||||
currentNode = currentNode.next;
|
|
||||||
count += 1;
|
|
||||||
}
|
|
||||||
if (currentNode) {
|
|
||||||
newNode.next = currentNode.next;
|
|
||||||
currentNode.next = newNode;
|
|
||||||
} else {
|
|
||||||
if (this.tail) {
|
|
||||||
this.tail.next = newNode;
|
|
||||||
this.tail = newNode;
|
|
||||||
} else {
|
|
||||||
this.head = newNode;
|
|
||||||
this.tail = newNode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -239,7 +244,9 @@ export default class LinkedList {
|
|||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
toString(callback) {
|
toString(callback) {
|
||||||
return this.toArray().map((node) => node.toString(callback)).toString();
|
return this.toArray()
|
||||||
|
.map((node) => node.toString(callback))
|
||||||
|
.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user