【Lintcode】113.Remove Duplicates from Sorted List II
2024-09-07 10:55:00
题目:
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
题解:
Solution 1 ()
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if (head == nullptr) {
return head;
}
ListNode* dummy = new ListNode(-); dummy->next = head;
head = dummy;
while (head->next != nullptr && head->next->next != nullptr) {
if (head->next->val == head->next->next->val) {
int value = head->next->val;
while (head->next != nullptr && head->next->val == value) {
head->next = head->next->next;
}
} else {
head = head->next;
}
} return dummy->next;
}
};
二级指针
Solution 2 ()
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if (head == nullptr || head->next == nullptr) {
return head;
}
ListNode **t = &head;
while (*t) {
if ((*t)->next && (*t)->next->val == (*t)->val) {
ListNode *tmp = *t;
while (tmp && (*t)->val == tmp->val) {
tmp = tmp->next;
}
*t = tmp;
} else {
t = &((*t)->next);
}
} return head;
}
};
最新文章
- java合并pdf
- 【新年呈献】高性能 Socket 组件 HP-Socket v3.1.2 正式发布
- java关键字 super 和 this
- HTML5系列二(标签元素、FileReader、拖放)
- ReentrantLock的实现语义与使用场景
- The available repos for opensuse13.2
- TextView的属性列表
- Error Dropping Database (Can't rmdir '.test\', errno: 17)
- LoadRunner界面分析(一)
- 修改info
- Android 根据屏幕分辨率自动调整字体大小
- 做一个有理想的IT人
- mybatis配置文件xxxx.xml中缺失返回类型的后果A query was run and no Result Maps were found
- 安装arm-linux-gcc交叉编译器
- JAVA加密算法系列-AES
- vue.js基础知识篇(4):过滤器、class与style的绑定2
- Rxjava + retrofit + dagger2 + mvp搭建Android框架
- windows服务器环境问题---api-ms-win-crt-runtimel1-1-0.dll缺失解决
- 马凯军201771010116《面向对象与程序设计Java》第十二周学习总结
- 4-3 组件参数校验与非props特性