My LeetCode grinding. Trying to do a problem a day.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Definition for singly-linked list.
  3. */
  4. function ListNode(val, next) {
  5. this.val = (val===undefined ? 0 : val)
  6. this.next = (next===undefined ? null : next)
  7. }
  8. /**
  9. * @param {ListNode} head
  10. * @return {ListNode}
  11. */
  12. var deleteDuplicates = function(head) {
  13. let prev = null;
  14. let current = head;
  15. let found = {};
  16. while (current != null) {
  17. if (current.val in found) {
  18. prev.next = current.next;
  19. current = current.next;
  20. } else {
  21. found[current.val] = true;
  22. prev = current;
  23. current = current.next;
  24. }
  25. }
  26. return head;
  27. };
  28. const printLinkedList = (ll) => {
  29. while (ll != null) {
  30. console.log(ll.val);
  31. ll = ll.next;
  32. }
  33. };
  34. console.log("Expected: 1->2");
  35. let ll = new ListNode(1, new ListNode(1, new ListNode(2)));
  36. console.log(`Got:`);
  37. printLinkedList(deleteDuplicates(ll));