# Remove Duplicates From Unsorted List Geeksforgeeks

## Remove Duplicates From Unsorted List Geeksforgeeks

## 题目地址

<https://www.geeksforgeeks.org/remove-duplicates-from-an-unsorted-linked-list/>

## 题目描述

```
Write a removeDuplicates() function which takes a list and deletes
any duplicate nodes from the list. The list is not sorted.

For example if the linked list is 12->11->12->21->41->43->21,
then removeDuplicates() should convert the list to 12->11->21->41->43.

If temporary buffer is not allowed, how to solve it?
```

## 代码

### Approach #1 Two loop

```java
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
    if (head == null) return null;

    ListNode curr = head;
    while (curr != null) {
      ListNode inner = curr;
      while (inner.next != null) {
        if (inner.next.val == curr.val) {
          inner.next = inner.next.next;
        } else {
          inner = inner.next;
        }
      }
      curr = curr.next;
    }

    return head;
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wentao-shao.gitbook.io/leetcode/linked-list/remove-duplicates-from-unsorted-list-geeksforgeeks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
