# Remove Duplicates From LinkedList

<figure><img src="https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-4c3824ccd5fd963884a7040309f29d18dc2c3fdc%2FScreenshot%202023-01-20%20at%2019.07.58.png?alt=media" alt=""><figcaption></figcaption></figure>

* create prevNode

```jsx
// This is an input class. Do not edit.
class LinkedList {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

function removeDuplicatesFromLinkedList(linkedList) {

  let currentNode = linkedList;
  let prevNode = new LinkedList();
  prevNode.next = currentNode;
  
  while (currentNode !== null) {
    if (prevNode.value !== currentNode.value) {
      prevNode = currentNode;
      currentNode = currentNode.next;
      continue;
    }
    
    prevNode.next = currentNode.next;
    currentNode = currentNode.next;
  }
  
  return linkedList;
}

// Do not edit the lines below.
exports.LinkedList = LinkedList;
exports.removeDuplicatesFromLinkedList = removeDuplicatesFromLinkedList;
```

* find next distinct node

```jsx
// This is an input class. Do not edit.
class LinkedList {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

function removeDuplicatesFromLinkedList(linkedList) {

  let currentNode = linkedList;
  while (currentNode !== null) {
    let nextDistinctNode = currentNode.next;
    while (nextDistinctNode !== null && nextDistinctNode.value === currentNode.value) {
      nextDistinctNode = nextDistinctNode.next;
    }
    
    currentNode.next = nextDistinctNode;
    currentNode = currentNode.next;
  }
  
  return linkedList;
}

// Do not edit the lines below.
exports.LinkedList = LinkedList;
exports.removeDuplicatesFromLinkedList = removeDuplicatesFromLinkedList;
```
