leetcode--Remove Duplicates from Sorted List
2016-04-28 15:21
393 查看
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.
拓展。删除数字中的重复数字。:
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head==null||head.next==null) return head; ListNode curr=head; while(curr.next!=null){ if(curr.val==curr.next.val){ curr.next=curr.next.next; } else{ curr=curr.next; } } return head; } }
拓展。删除数字中的重复数字。:
package test; import java.util.ArrayList; public class deleteDumpleNum { private static ArrayList deleDump(int[] a) { ArrayList<Integer> res = new ArrayList<Integer>(); if (a.length == 1) { res.add(a[0]); return res; } else { res.add(a[0]); for (int i = 1; i < a.length; i++) { if (a[i] != a[i - 1]) { res.add(a[i]); } } } return res; } public static void main(String[] args) { int[] a = { 1, 2, 3, 4, 5, 5, 6 }; ArrayList<Integer> out = deleDump(a); for (int i = 0; i < out.size(); i++) { System.out.print(out.get(i) + "\t"); } } }
相关文章推荐
- 如何使用jquery修改css中带有!important的样式属性
- 如何在改版后的spring官网下载spring
- selenium - 驱动 chromedriver 初始化问题总结
- 5-15 QQ帐户的申请与登陆 (25分)
- 技术入股的坑
- Android onTouch事件解析
- 对象数组与指针+this指针
- 装饰者模式--Java篇
- NC开发笔记——1、主子表增加页签
- iPad登录网站,隐藏上传文档按钮
- RxJava学习(八)
- WebStorm2016.1 破解 激活
- SASS界面编译工具——Koala的使用
- Android http 请求——AsyncHttpClient集成【解决重定向问题、setEnableRedirects 方法异常、线程中调用异常问题】
- 九 循环
- docker 报Error: docker-engine-selinux conflicts with docker-selinux-1.9.1-25.el7.centos.x86_64
- equals函数的作用
- bootstrap的编辑标记 angularjs input 弹出框
- Python爬虫实战:极客学院
- 巴克莱银行如何利用R3区块链技术构建智能合约模板?