# LeetCode: Remove Duplicates from Sorted List Solution

Keep jump over next node until values are not equal

## Approach

Keep jump over next node until values are not equal

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var deleteDuplicates = function (head) {2  let node = head3
4  while (node) {5    while (node.next && node.val === node.next.val) {6      node.next = node.next.next7    }8    node = node.next9  }10

## Implementation (recursive)

Note the recursive solution

• it would be less painful to understand if we draw it down
• one thing to remember,
return
statement of first call will run last
`1var deleteDuplicates = function (head) {2  if (!head) return null3  head.next = deleteDuplicates(head.next)4  return head.next && head.next.val === head.val ? head.next : head5}`

leetcode

recursion

## Next Post

LeetCode: Check If String Is a Prefix of Array

Aug 27, 2021

Accumulated concatenation and check

## Previous Post

LeetCode: Binary Tree Inorder Traversal

Aug 26, 2021

Recursive or iterative (stack)

Search Posts