Created
March 2, 2017 18:39
-
-
Save vitkarpov/a602426420d443452dc99b3ca5ef9ebe to your computer and use it in GitHub Desktop.
"Cracking the coding interview". Linked lists, 1.1
This file contains hidden or 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
/** | |
* Удаляет дубликаты из связаного списка. | |
* Функция принимает на вход указатель на начало списка. | |
* | |
* @param {Node} head | |
* @returns {Node} | |
*/ | |
function removeNode(head) { | |
// Предполагается, что head объект вида { data: <*>, next: <Node> } | |
const hash = {}; | |
let current = head; | |
let prev = null; | |
while (current !== null) { | |
if (hash[current.data]) { | |
prev.next = current.next; | |
} else { | |
hash[current.data] = true; | |
prev = current; | |
} | |
current = current.next; | |
} | |
return head; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment