Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vitkarpov/a602426420d443452dc99b3ca5ef9ebe to your computer and use it in GitHub Desktop.
Save vitkarpov/a602426420d443452dc99b3ca5ef9ebe to your computer and use it in GitHub Desktop.
"Cracking the coding interview". Linked lists, 1.1
/**
* Удаляет дубликаты из связаного списка.
* Функция принимает на вход указатель на начало списка.
*
* @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