I、Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->2->3.

PS:遍历,而后记录pre,并删除后续重复node

 /**
* 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) {
ListNode *cur=head,*pre=NULL;
while(cur!=NULL){
if(pre==NULL){
pre=cur;
cur=cur->next;
continue;
}
ListNode *next=cur->next;
if(pre->val==cur->val){
pre->next=next;
}else
pre=cur;
cur=next;
}
return head;
}
};

II、

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.

删除所有重复项。

PS:循环比较next->val==cur->val,若next跳动则剔除cur至next之间的节点。否则右移left指针。

 /**
* 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 h(-);
ListNode *res=&h;
res->next=head;
ListNode *cur=head,*pre=NULL,*left=res;
while(cur!=NULL){
ListNode* next=cur->next;
while(next!=NULL&&cur->val==next->val){
next=next->next;
}
if(next==cur->next){
left=cur;
}else{
left->next=next;
}
cur=next;
}
return res->next;
}
};

remove-duplicates-from-sorted-list I&II——去除链表中重复项的更多相关文章

  1. LeetCode 83. Remove Duplicates from Sorted List (从有序链表中去除重复项)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  3. [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  4. 【LeetCode每天一题】Remove Duplicates from Sorted List(移除有序链表中的重复数字)

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  5. [LC]83题 Remove Duplicates from Sorted List(删除排序链表中的重复元素)(链表)

    ①英文题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exa ...

  6. 力扣—Remove Duplicates from Sorted List(删除排序链表中的重复元素)python实现

    题目描述: 中文: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2 示例 2: 输入: 1->1->2 ...

  7. LeetCode 83. Remove Duplicates from Sorted List(从有序链表中删除重复节点)

    题意:从有序链表中删除重复节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode ...

  8. [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项

    2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...

  9. LeetCode:Remove Duplicates from Sorted List I II

    LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...

随机推荐

  1. C#如何定义一个变长的一维和二维数组

    1.假设将要定义数组的长度为程序执行过程中计算出来的MAX List<int> Arc = new List<int>(); ; i < MAX; i++) { Arc. ...

  2. Ubuntu 查看默认软件安装位置

    tags: Linux 方法 1:在命令行输入:dpkg -L 软件包名: 方法 2:在/var/cache/apt/archives找的你安装程序的包,然后用gdebi-gtk+软件包名可以查看具体 ...

  3. 分布式存储ceph集群实践

    1.环境规划,三台主机 10.213.14.51/24            10.213.14.52/24       10.213.14.53/24    集群网络 172.140.140.11. ...

  4. hdu 4501 多维0-1背包

    小明系列故事——买年货 Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  5. 分配问题(cogs 740)

    «问题描述: 有n件工作要分配给n个人做.第i 个人做第j 件工作产生的效益为c[i][j]  .试设计一个将n件工作分配给n个人做的分配方案,使产生的总效益最大. «编程任务: 对于给定的n件工作和 ...

  6. 学习 表单验证插件validate

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 把握linux内核设计思想系列【转】

    转自:http://blog.csdn.net/shallnet/article/details/47734053 版权声明:本文为博主原创文章,未经博主允许不得转载.如果您觉得文章对您有用,请点击文 ...

  8. Linux 6.x 下Oracle 11g R2 安装配置

    Oracle 11g R2 数据库安装硬件配置要求: 最小内存 1 GB of RAM 虚拟内存容量,这个oracle也有要求,不用担心此时的swap分区不够oracle的要求 .虚拟内存swap如何 ...

  9. Python学习杂记_4_分支和循环

    分支 和 循环 分支和循环这俩结构在各语言中都有着很重要的地位,当然我之前都没有学好,这里总结一下在Python学习中对这俩结构的认识. 分支结构 # 单分支 if 条件判断: 执行语句… # 双分支 ...

  10. Android 代码里设置ImageView的src和background

    设置ImageView的src: image.setImageDrawable(getResources().getDrawable(R.drawable.blackk)); String path= ...