博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Remove Duplicates from Sorted List II
阅读量:4352 次
发布时间:2019-06-07

本文共 1182 字,大约阅读时间需要 3 分钟。

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.

 

1 public class Solution { 2     public ListNode deleteDuplicates(ListNode head) { 3         if(head==null || head.next==null) return head; 4         ListNode safe = new ListNode(-1); 5         safe.next = head; 6         ListNode pre = safe; 7         ListNode cur = head; 8         ListNode post = head.next; 9         while(post!=null){10             if(post.val==cur.val){11                 while(post!=null && post.val==cur.val){12                     cur = post;13                     post = post.next;14                 }15                 cur = post;16                 if(post!=null)17                     post = post.next;18                 continue;19             }20              pre.next = cur;21              pre = cur;22              cur = post;23              if(post!=null)24                 post = post.next;25         }26         pre.next = cur;27         return safe.next;28     }29 }
View Code

 

 

 

转载于:https://www.cnblogs.com/krunning/p/3538777.html

你可能感兴趣的文章
Python学习之==>条件判断
查看>>
IIS7 配置PUT请求 NET
查看>>
JQuery 对表格的详细操作
查看>>
jetty上传 Form too large: 275782 > 200000
查看>>
不定个数的输入数字 并做复数运算
查看>>
A week of egg yoghourt diet, 7 days off 16 pounds of fat
查看>>
メイプルストーリー、「魔法少女まどか☆マギカ」とのタイアップを実施
查看>>
Knockout.Js官网学习(系列)
查看>>
Win8 连连看小程序
查看>>
例3-1和例3-2
查看>>
Centos7安装net Core
查看>>
cogs1538 [AHOI2005]LANE 航线规划
查看>>
[AaronYang]C#人爱学不学[3]
查看>>
使用eclipse学习java第一课
查看>>
1047 编程团体赛
查看>>
java 基本程序设计结构 一
查看>>
大数据概述
查看>>
SpringCloud-Config分布式配置中心
查看>>
c++ string 结束符‘\000’
查看>>
Js 自定义日期格式的正则表达式验证
查看>>