LeetCode --- 82. Remove Duplicates from Sorted List II
来源:互联网 发布:com后缀的域名 编辑:程序博客网 时间:2024/05/18 02:13
题目链接:Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
这道题的要求是在有序链表中删除有重复数字的节点,留下没有重复的节点。
又是先出现的II题目,是Remove Duplicates from Sorted List的扩展,其实这俩题也都差不多,基础的链表操作。
思路就是找到重复元素,删除即可。如果两个连续节点的数字一样,则循环检测,删除重复的元素,不过最后还需要删除该重复节点一下,因为要求不保留重复节点。
时间复杂度:O(n)
空间复杂度:O(1)
1 class Solution 2 { 3 public: 4 ListNode *deleteDuplicates(ListNode *head) 5 { 6 ListNode *h = new ListNode(0), *p = h; 7 h -> next = head; 8 9 while(p -> next != NULL)10 {11 if(p -> next -> next != NULL && 12 p -> next -> val == p -> next -> next -> val)13 {14 while(p -> next -> next != NULL && 15 p -> next -> val == p -> next -> next -> val)16 p -> next = p -> next -> next;17 p -> next = p -> next -> next;18 }19 else20 p = p -> next;21 }22 23 return h -> next;24 }25 };
转载请说明出处:LeetCode --- 82. Remove Duplicates from Sorted List II
0 1
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List II
- [leetcode]Remove Duplicates from Sorted List II
- LeetCode-Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- LeetCode - Remove Duplicates from Sorted List II
- LeetCode | Remove Duplicates from Sorted List II
- LeetCode:Remove Duplicates from Sorted List II
- 【leetcode】Remove Duplicates from Sorted List II
- Leetcode: Remove Duplicates from Sorted List II
- leetcode Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- Flash and Scalform CLIK
- Android4.4 SystemUI分析之Clock时钟显示
- 再看数据库——(7)游标
- poj 169
- 51小东西霍尔计数器
- LeetCode --- 82. Remove Duplicates from Sorted List II
- 第四周程序阅读(4)
- 判断./configure,make等长输出命令是否执行成功
- 网络编程socket之listen函数
- win7 Qt-5.3.1 cmake-2.8.11.2 opencv-2.4.11平台搭建
- rabbitmq最大连接数(Socket Descriptors)(转)
- 拷贝构造函数
- Ubuntu上Deb包的生成
- JAVASCRIPT实现基于文本的自动智能聊天机器人