[LeetCode] 83. Remove Duplicates from Sorted List
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
Example 1:
Input: head = [1,1,2]
Output: [1,2]
Example 2:
Input: head = [1,1,2,3,3]
Output: [1,2,3]
Constraints:
The number of nodes in the list is in the range [0, 300].
-100 <= Node.val <= 100
The list is guaranteed to be sorted in ascending order.
删除排序链表中的重复元素。
给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。
思路
思路很简单,每次去看一下当前节点和下一个节点的val是否相同,如是,则跳过下一个节点。
复杂度
时间O(n)
空间O(1)
代码
Java实现
1 |
|
JavaScript实现
1 |
|
相关题目
1 |
|
[LeetCode] 83. Remove Duplicates from Sorted List
https://shurui91.github.io/posts/3569698204.html