> For the complete documentation index, see [llms.txt](https://liuxue2010.gitbook.io/data-structure-and-algorithms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuxue2010.gitbook.io/data-structure-and-algorithms/linked-list/remove-duplicates-from-sorted-list.md).

# Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

## Example

Given 1->1->2, return 1->2.

Given 1->1->2->3->3, return 1->2->3.

## Solution

删除操作一定是node.next = node.next.next

```
"""
Definition of ListNode
class ListNode(object):
    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""
class Solution:
    """
    @param head: A ListNode
    @return: A ListNode
    """
    def deleteDuplicates(self, head):
        # write your code here
        if head == None:
            return head

        node = head

        while node and node.next:
            if node.val == node.next.val:
                node.next = node.next.next
            else:
                node = node.next

        return head
```
