Created
November 11, 2018 23:47
-
-
Save AlexeiDarmin/2ff9a79a91b884bc485a00eb6621078f to your computer and use it in GitHub Desktop.
Remove duplicates from a linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deleteDuplicates(head: Node): Node { | |
const occurredVals = new Map() | |
let node = head | |
occurredVals.set(node.data, true) | |
while (node.next != null) { | |
if (occurredVals.get(node.next.data) === true) { | |
node.next = node.next.next | |
} else { | |
occurredVals.set(node.next.data, true) | |
node = node.next | |
} | |
} | |
return head | |
} | |
class Node { | |
next:any | |
data: number | |
constructor(num: number) { | |
this.data = num | |
} | |
appendToTail = (num: number) => { | |
const newNode = new Node(num) | |
let node = this | |
while (node.next != null) { | |
node = node.next | |
} | |
node.next = newNode | |
} | |
} | |
/* | |
const node1 = new Node(1) | |
node1.appendToTail(2) | |
node1.appendToTail(1) | |
node1.appendToTail(1) | |
node1.appendToTail(3) | |
node1.appendToTail(3) | |
console.log(deleteDuplicates(node1)) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment