[JavaScript] Easy to understand - 2 solutions - detailed explanation
Core Strategy\n\nThe important points for this problem are:\n- the original linked list is sorted\n- we need to remove all the duplicate number, rather than lef
## Core Strategy\n\nThe important points for this problem are:\n- the original linked list is sorted\n- we need to remove all the duplicate number, rather than left one there\n\nSo, what we need to do is:\n- find a way to check one value is duplicated or not\n- traverse the linked list and remove the duplicate nodes\n- make sure we can still get the head of the final linked list\n\nFor step 2, we just use loop to traverse the linked list.\nFor step 3, we could use a dummy node as the new head, to let us easier remove the old head node if we need.\nFor step 1, here are 2 solutions with extra space or not.\n\n## With extra space\n\nFor this solution, we use a hashtable to do the counting for values in linked list. Then traverse again and remove the nodes we don\'t need.\n\nHere\'s a sample code from me:\n\n```js\nconst deleteDuplicates = head => {\n  const count = {};\n  const dummy = { next: head };\n  for (let cur = head; cur; cur = cur.next) {\n    count[cur.val] = (count[cur.val] || 0) + 1;\n  }\n  for (let cur = dummy; cur.next;) {\n    count[cur.next.val] > 1 ? (cur.next = cur.next.next) : (cur = cur.next);\n  }\n  return dummy.next;\n};\n```\n\n## Without extra space\n\nFor this solution, since we don\'t want to use extra space, so we need to maintain 3 pointers - `prev`, `cur` and `next`. By this, we could compare the value and remove the nodes easily.\n\nI use the optional chaining operator (`?.`) in JS to avoid more checking logic.\n\nHere\'s a sample code from me:\n\n```js\nconst deleteDuplicates = head => {\n  const dummy = { next: head };\n  let prev = dummy;\n  let cur = head;\n  let next = head?.next;\n  while (next) {\n    if (cur.val !== next.val) {\n      prev = cur;\n      cur = next;\n      next = next.next;\n    } else {\n      const val = cur.val;\n      while (next && next.val === val) {\n        next = next.next;\n      }\n      cur = next;\n      next = next?.next;\n      prev.next = cur;\n    }\n  }\n  return dummy.next;\n};\n```
['JavaScript']