题目:

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;
}
};

 

最新文章

  1. java合并pdf
  2. 【新年呈献】高性能 Socket 组件 HP-Socket v3.1.2 正式发布
  3. java关键字 super 和 this
  4. HTML5系列二(标签元素、FileReader、拖放)
  5. ReentrantLock的实现语义与使用场景
  6. The available repos for opensuse13.2
  7. TextView的属性列表
  8. Error Dropping Database (Can't rmdir '.test\', errno: 17)
  9. LoadRunner界面分析(一)
  10. 修改info
  11. Android 根据屏幕分辨率自动调整字体大小
  12. 做一个有理想的IT人
  13. mybatis配置文件xxxx.xml中缺失返回类型的后果A query was run and no Result Maps were found
  14. 安装arm-linux-gcc交叉编译器
  15. JAVA加密算法系列-AES
  16. vue.js基础知识篇(4):过滤器、class与style的绑定2
  17. Rxjava + retrofit + dagger2 + mvp搭建Android框架
  18. windows服务器环境问题---api-ms-win-crt-runtimel1-1-0.dll缺失解决
  19. 马凯军201771010116《面向对象与程序设计Java》第十二周学习总结
  20. 4-3 组件参数校验与非props特性

热门文章

  1. python 使用微信远程控制电脑
  2. C 语言学习 3
  3. Urho3D 在Win10下编辑器崩溃的解决方案
  4. Pandoc PDF 中文
  5. centos安装python3.7.0过程记录
  6. coreos 安装
  7. jQuery+bootstrap实现美化警告/确认/提示对话框插件
  8. iPhone,iPad如何获取WIFI名称即SSID
  9. myql 5.6 安装
  10. EasyNVR H5直播流媒体解决方案前端构建之:如何播放自动适配RTMP/HLS直播播放