diff --git a/chapter02/2.1 - Remove Dups/removeDups.js b/chapter02/2.1 - Remove Dups/removeDups.js index 1a34d5b..4068cf6 100644 --- a/chapter02/2.1 - Remove Dups/removeDups.js +++ b/chapter02/2.1 - Remove Dups/removeDups.js @@ -1,82 +1,76 @@ -/* CLASS */ -var LinkedList = function(value) { - this.value = value; - this.next = null; -}; +//O(n2) time & O(1) space with no buffer -/* FUNCTIONS */ -var checkDups = function(head, node) { - var currNode = head; - while (currNode !== node) { - if (currNode.value === node.value) { - return true; - } - currNode = currNode.next; +class Node { + constructor(data) { + this.data = data; + this.next = null; } - return false; -}; +} -var printLinkedList = function(head) { - var node = head; - console.log('start of linked list'); - while (node !== null) { - console.log(node.value); - node = node.next; +class SinglyLinkedList { + constructor() { + this.head = null; + this.length = 0; } - console.log('end of linked list'); -}; -var removeDups = function(head) { - var node = head; - while (node !== null) { - if (node.next !== null && checkDups(head, node.next)) { - node.next = node.next.next; + appendToTail(data) { + let node = new Node(data); + //if empty, set new node to head + if (this.length === 0) { + this.head = node } else { - node = node.next; + //start at head and go through until reach null tail + let current = this.head; + while(current.next != null) { + current = current.next + } + current.next = node; } + this.length++; } - return head; -}; - -/* TESTS */ -var a = new LinkedList('a'); -var b = new LinkedList('b'); -var c = new LinkedList('c'); -var d = new LinkedList('d'); -var e = new LinkedList('e'); - -a.next = b; -b.next = c; -c.next = d; -d.next = e; - -removeDups(a); -printLinkedList(a); - -var f = new LinkedList('f'); -var g = new LinkedList('g'); -var h = new LinkedList('g'); -var i = new LinkedList('g'); -var j = new LinkedList('g'); - -f.next = g; -g.next = h; -h.next = i; -i.next = j; -removeDups(f); -printLinkedList(f); - -var k = new LinkedList('g'); -var l = new LinkedList('g'); -var m = new LinkedList('g'); -var n = new LinkedList('b'); -var o = new LinkedList('g'); + printList() { + //if empty list + if (this.length === 0) { + console.log('List Empty') + } else { + //first print head data, then loop through print until null tail + let current = this.head; + console.log(current.data) + while(current.next != null) { + console.log(current.next.data) + current = current.next + } + } + } -k.next = l; -l.next = m; -m.next = n; -n.next = o; + removeDups() { + //make hash map while looping through list + let current = this.head; + //start at head loop through until reach null tail + while(current != null) { + let runner = current + //make another runner pointer + while(runner.next != null) { + if (current.data === runner.next.data) { + runner.next = runner.next.next; + this.length--; + } else { + runner = runner.next; + } + } + current = current.next + } + } +} -removeDups(k); -printLinkedList(k); +let linkyList = new SinglyLinkedList(); +linkyList.appendToTail('a'); +linkyList.appendToTail('p'); +linkyList.appendToTail('p'); +linkyList.appendToTail('l'); +linkyList.appendToTail('e'); +linkyList.printList(); +console.log('Remove Duplicates') +linkyList.removeDups(); +linkyList.printList(); diff --git a/chapter02/2.3 - Delete Middle Node/deleteMiddleNode.js b/chapter02/2.3 - Delete Middle Node/deleteMiddleNode.js index 6ead6dd..bc24456 100644 --- a/chapter02/2.3 - Delete Middle Node/deleteMiddleNode.js +++ b/chapter02/2.3 - Delete Middle Node/deleteMiddleNode.js @@ -1,48 +1,39 @@ -var LinkedList = function(value) { - this.value = value; - this.next = null; -}; - -var deleteMidNode = function(midNode) { - var node = midNode; - while (node !== null && node.next !== null) { - node.value = node.next.value; - if (node.next.next === null) { - node.next = null; - } - node = node.next; +//delete node in the middle of linked list +class linkedList { + constructor(data) { + this.data = data + this.next = null } -}; +} + +const deleteNode = (nodeDel) => { + nodeDel.data = nodeDel.next.data + nodeDel.next = nodeDel.next.next + //delete next node from emmory -// a -> b -> c -> d -> e -> f, input c -// a -> b -> *d -> d -> e -> f -// a -> b -> d -> *e -> e -> f -// a -> b -> d -> e -> *f -> f -// a -> b -> d -> e -> f -> *null +} -/* TEST */ -var printList = function(head) { - while(head !== null) { - console.log(head.value); - head = head.next; +const printList = (headNode) => { + let current = headNode; + while(current != null) { + console.log(current.data) + current = current.next } - console.log('done printing'); -}; +} -var a = new LinkedList('a'); -var b = new LinkedList('b'); -var c = new LinkedList('c'); -var d = new LinkedList('d'); -var e = new LinkedList('e'); -var f = new LinkedList('f'); +// create new LL nodes +let a = new linkedList('1'); +let b = new linkedList('2'); +let c = new linkedList('3'); +let d = new linkedList('4'); +let e = new linkedList('5'); -a.next = b; -b.next = c; -c.next = d; -d.next = e; -e.next = f; +//link nodes +a.next = b +b.next = c +c.next = d +d.next = e -printList(a); -deleteMidNode(c); -printList(a); +deleteNode(d, a) +printList(a) \ No newline at end of file