83. 删除排序链表中的重复元素

问题描述

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例 1:
输入: 1->1->2
输出: 1->2
示例 2:
输入: 1->1->2->3->3
输出: 1->2->3

代码(快慢指针法)

* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head ==NULL)return NULL;
ListNode* slow=head,*fast=head;
while(fast)
{
if(slow->val != fast->val)
{
slow = slow->next;
slow->val = fast->val;
fast = fast->next;
}
else{
fast = fast->next;
}
}
slow->next = NULL;
return head;
}
};

结果:

执行用时 :12 ms, 在所有 cpp 提交中击败了94.60%的用户
内存消耗 :9.1 MB, 在所有 cpp 提交中击败了65.38%的用户

代码2(便于和下一个问题类比)

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head ==NULL || head->next == NULL)return head;
ListNode* ans = new ListNode(0),*pre = ans,*cur;
pre->next = head;
while(pre->next!= NULL)
{
cur = pre->next;
while(cur->next && cur->val == cur->next->val)cur=cur->next;
if(cur != pre->next)pre->next = cur;
else{
pre = pre->next;
}
}
return ans->next;
}
};

结果:

执行用时 :8 ms, 在所有 C++ 提交中击败了96.49%的用户
内存消耗 :13.4 MB, 在所有 C++ 提交中击败了5.01%的用户

82. 删除排序链表中的重复元素 II

问题描述

给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
输入: 1->2->3->3->4->4->5
输出: 1->2->5
示例 2:
输入: 1->1->1->2->3
输出: 2->3

代码

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head == NULL || head->next == NULL)return head;
ListNode* ans = new ListNode(0),*pre = ans,*cur;
ans->next = head;
while(pre->next)
{
cur = pre->next;
while(cur->next && cur->val == cur->next->val)cur = cur->next;//去除重复元素
if(cur != pre->next)pre->next = cur->next;//如果存在重复元素,则指向重复元素下一位
else pre = pre->next;//不存在重复元素指向目前元素的下一位即可
}
return ans->next;
}
};

结果:

执行用时 :8 ms, 在所有 C++ 提交中击败了88.88%的用户
内存消耗 :13.1 MB, 在所有 C++ 提交中击败了5.01%的用户

最新文章

  1. Redis_redis分布式锁-SETNX
  2. Python-Django进阶
  3. 利用JS制作简便计算器
  4. SQL实现将一个表的数据插入到另外一个表的代码
  5. SQL2005中的事务与锁定(一) - 转载
  6. java properties读取与设值
  7. 常州培训 day6 解题报告
  8. [SQL]开启事物,当两条插入语句有出现错误的时候,没有错误的就插入到表中,错误的语句不影响到正确的插入语句
  9. 【转】NSDictionary以及NSMutableDictionary的用法
  10. ajaxError
  11. Linux——搭建PHP开发环境第四步:composer
  12. openGL learning
  13. 解决exlipse下 springboot 错误:找不到或无法加载主类
  14. [Swift]LeetCode39. 组合总和 | Combination Sum
  15. php伪造ip头
  16. <iframe width="250" height="250" src="http://www.baidu.com"></iframe>
  17. CodeForces7D 字符串hash + dp
  18. object references an unsaved transient instance save the transient instance before flushing
  19. Altium Designer 规则设置
  20. L238

热门文章

  1. 月薪过2w的IT程序员都是怎么做到的?
  2. 有个性的手动计划模式(Project)
  3. 制作一个文档同步工具,自动同步到gitee中。。。
  4. 【LeetCode】66. Plus One 解题报告(Python)
  5. 【LeetCode】504. Base 7 解题报告(Java & Python)
  6. 【LeetCode】872. Leaf-Similar Trees 解题报告(Python)
  7. UVA11754 - Code Feat
  8. wordpress中遇到的问题
  9. 使用pynput同时监听鼠标和键盘
  10. [opencv]使用g++编译opencv程序演示