LeetCode 0083 - Remove Duplicates from Sorted List
# Hints
- 链表 + 水题
# 题面
Difficulty | Time Complexity Limit | Extra-Memory Complexity Limit |
---|---|---|
Easy |
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
# 题意
给定一个有序链表,要求去除其中的重复元素。
# 题解
水题,按题意模拟即可。
# AC代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode * next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode * next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode * deleteDuplicates(ListNode * head) {
// 特判
if (head == nullptr) {
return head;
}
// 求解
ListNode * p = head;
while (p != nullptr && p->next != nullptr) {
// 去重
while (p != nullptr && p->next != nullptr && p->val == p->next->val) {
//ListNode * q = p->next;
p->next = p->next->next;
//delete q;
}
// 递进
p = p->next;
}
// 返回
return head;
}
};
- 01
- Reading Papers - Kernel Concurrency06-01
- 02
- Linux Kernel - Source Code Overview05-01
- 03
- Linux Kernel - Per-CPU Storage05-01