目录结构
1.题目
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例:
输入: 1->1->2
输出: 1->2
输入: 1->1->2->3->3
输出: 1->2->3
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2.题解
直接遍历判断即可。
public class Solution83 {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode pre = head, p = head.next;
while (p != null) {
if (pre.val == p.val) {
pre.next = p.next;
}else{
pre = pre.next;
}
p = p.next;
}
return head;
}
}
- 时间复杂度:
- 空间复杂度: