LeetCode Remove Duplicates from Sorted List II
2016-06-07 18:07
253 查看
题意:给出一个单链表 ,将其中重复的元素删除
思路:在找重复结点时,需要找到其前继结点
代码如下:
class Solution
{
public ListNode deleteDuplicates(ListNode head)
{
ListNode cur = head, next = null, prev = null;
if (null == head) return head;
for (; cur != null; )
{
boolean found = false;
for (next = cur.next; next != null; next = next.next)
{
if (cur.val != next.val)
{
break;
}
else found = true;
}
if (found)
{
if (null == prev)
{
head = next;
cur = head;
}
else
{
prev.next = next;
cur = next;
}
}
else
{
prev = cur;
cur = cur.next;
}
}
return head;
}
}
思路:在找重复结点时,需要找到其前继结点
代码如下:
class Solution
{
public ListNode deleteDuplicates(ListNode head)
{
ListNode cur = head, next = null, prev = null;
if (null == head) return head;
for (; cur != null; )
{
boolean found = false;
for (next = cur.next; next != null; next = next.next)
{
if (cur.val != next.val)
{
break;
}
else found = true;
}
if (found)
{
if (null == prev)
{
head = next;
cur = head;
}
else
{
prev.next = next;
cur = next;
}
}
else
{
prev = cur;
cur = cur.next;
}
}
return head;
}
}
相关文章推荐
- 在Linux下启动Oracle
- CocoaPods报错:The dependency `AFNetworking ` is not used in any concrete target
- 分布式系统一致性的发展历史
- 五指cms筛选功能的实现
- sublime3修改选中颜色
- CUsparse ch3 Cusparse索引及其数据格式
- urlencode
- OC textField键盘弹起事件
- Linux Ubantu安全模式修改root密码
- SharePoint 2013 弹窗效果之URL打开方式(一)
- 安卓中为了获取context的方法和区别(getContext,getActivity,this,mainActivity.this等)
- jquery 获取 动态input等元素的id
- css3学习--border
- runtime基础
- 2016河南ACM省赛-A-表达式求值
- 博客地址已经搬家
- 虚拟机中CentOS6.8非图形界面配置IP
- JVM参数大全
- spark查询elasticsearch
- mybatis的模糊查询