Leecode Remove Duplicates from Sorted List
来源:互联网 发布:淘宝默认好评是几天 编辑:程序博客网 时间:2024/06/10 04:19
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.
class Node{private int val; Node next;public Node(int val) {this.val = val;this.next = null;}}
public Node removeduplicate(Node head){Node p = head ;Node cur = head ;while(p.next!=null){if(p.next.val!=p.val){cur.next = p.next;cur = p.next;}p = p.next;}cur.next = p.next; return head;}
我们把相邻两数不相等的地方称为边界,再用cur指针指向边界的开始处,当p指针在前进的过程中遇到下一个边界时去掉中间部分
下面的方法更好,
public class Solution { public ListNode deleteDuplicates(Node head) { if(head == null || head.next == null) return head; Node p = head; while( p!= null && p.next != null){ if(p.val == p.next.val){ p.next = p.next.next; }else{ p = p.next; } } return head; }}
0 0
- Leecode Remove Duplicates from Sorted List
- LeeCode-Remove Duplicates from Sorted List
- LeeCode 82 Remove Duplicates from Sorted List II
- leecode 解题总结:82. Remove Duplicates from Sorted List II
- leecode 解题总结:83. Remove Duplicates from Sorted List
- [LeeCode] 82. Remove Duplicates from Sorted List II java
- LeeCode-Remove Duplicates from Sorted Array II
- leecode-Remove Duplicates from Sorted Array
- Leecode-Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove duplicates from sorted list
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- 第7周作业2-极值计算
- Extjs4-----布局 layout 详解1-accordion(折叠)
- 一固定IP地址在iis 7.0 下实现多域名共用80端口
- Unity3d Http Get请求
- C#常用正则表达整理
- Leecode Remove Duplicates from Sorted List
- 图书管理系统——创建菜单栏(使用JMenuBar创建菜单栏)
- 【C++】类型强制转换的注意事项
- 游戏架构 云游戏(3)-游戏运维
- hadoop作业调优参数整理及原理
- JVM里面hashtable和hashmap实现原理
- qq空间营销方案讲解
- Struts2标签页面取值
- Android Bitmap 蒙版使用