diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index c7e075b0..ef7560da 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -2,8 +2,6 @@ import LinkedListNode from './LinkedListNode'; import Comparator from '../../utils/comparator/Comparator'; export default class LinkedList { - head: any; - tail: any; /** * @param {Function} [comparatorFunction] */ @@ -281,20 +279,20 @@ export default class LinkedList { let node2Prev = null; let node1 = this.head; let node2 = this.head; - + // elements are same, no swap to be made if (data1 === data2) { return; } - + while (node1 !== null) { - if (node1.data === data1) { + if (node1.data === data1) { break; } node1Prev = node1; node1 = node1.next; } - + while (node2 !== null) { if (node2.data === data2) { break; @@ -302,28 +300,26 @@ export default class LinkedList { node2Prev = node2; node2 = node2.next; } - + // swap not possible, one or more element not in list if (node1 === null || node2 === null) { return; } - + if (node1Prev === null) { this.head = node2; } else { node1Prev.setNextNode(node2); } - - if (node2Prev === null) { + + if (node2Prev === null) { this.head = node1; } else { node2Prev.setNextNode(node1); } - - let temp = node1.next; + + const temp = node1.next; node1.setNextNode(node2.next); - node2.setNextNode(temp); + node2.setNextNode(temp); } - - }