Ver código fonte

New problem done

master
Lachlan Jacob 5 anos atrás
pai
commit
c5a83a58ed
3 arquivos alterados com 56 adições e 0 exclusões
  1. 40
    0
      problems/83/main.js
  2. 13
    0
      problems/83/problem.txt
  3. 3
    0
      problems/83/run.sh

+ 40
- 0
problems/83/main.js Ver arquivo

@@ -0,0 +1,40 @@
/**
* Definition for singly-linked list.
*/
function ListNode(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}

/**
* @param {ListNode} head
* @return {ListNode}
*/
var deleteDuplicates = function(head) {
let prev = null;
let current = head;
let found = {};
while (current != null) {
if (current.val in found) {
prev.next = current.next;
current = current.next;
} else {
found[current.val] = true;
prev = current;
current = current.next;
}
}
return head;
};

const printLinkedList = (ll) => {
while (ll != null) {
console.log(ll.val);
ll = ll.next;
}
};

console.log("Expected: 1->2");
let ll = new ListNode(1, new ListNode(1, new ListNode(2)));
console.log(`Got:`);
printLinkedList(deleteDuplicates(ll));

+ 13
- 0
problems/83/problem.txt Ver arquivo

@@ -0,0 +1,13 @@
Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3



+ 3
- 0
problems/83/run.sh Ver arquivo

@@ -0,0 +1,3 @@
#!/bin/bash

node main.js

Carregando…
Cancelar
Salvar