# [Remove Duplicates from Sorted List][title] ## Description Given a sorted linked list, delete all duplicates such that each element appear only *once*. **Example 1:** ``` Input: 1->1->2 Output: 1->2 ``` **Example 2:** ``` Input: 1->1->2->3->3 Output: 1->2->3 ``` **Tags:** Linked List ## 思路 题意是删除链表中重复的元素,很简单,我们只需要遍历一遍链表,遇到链表中相邻元素相同时,把当前指针指向下下个元素即可。 ```java /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null || head.next == null) return head; ListNode curr = head; while (curr.next != null) { if (curr.next.val == curr.val) { curr.next = curr.next.next; } else { curr = curr.next; } } return head; } } ``` ## 结语 如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-java-leetcode][ajl] [title]: https://leetcode.com/problems/remove-duplicates-from-sorted-list [ajl]: https://github.com/Blankj/awesome-java-leetcode