Remove Duplicates from Sorted List
来源:互联网 发布:龙之信条萝莉捏脸数据 编辑:程序博客网 时间:2024/06/08 16:37
Given a sorted linked list, delete all duplicates such that each element appear onlyonce.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
思路: 判断是否重复,就需要用双指针,利用pre和cur两个指针来判断。如果相等,就代表有重复,那么用delete的操作来delete重复点,那么就是pre.next = cur.next;来delete掉cur的点。如果不相等,那么pre进行移动。pre = cur;
/** * 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 dump = new ListNode(0); dump.next = head; ListNode pre = head; while(pre!=null && pre.next!=null){ ListNode cur = pre.next; if(pre.val == cur.val){ pre.next = cur.next; } else { pre = cur; } } return dump.next; }}
0 0
- 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
- 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
- 键、索引、约束及其区别
- java 解析json 遍历未知key
- The Snail
- 软连接的相对路径问题
- hadoop_hellow_word
- Remove Duplicates from Sorted List
- Python2.7.3读取日志信息时发生读取不完整的问题
- 修改开源代码的configure与makefile
- Qemu创建KVM虚拟机内存初始化流程
- securecrt + vim + solarized
- Linux Namespace
- tomcat部署web项目的3中方法
- oracle查询当前系统时间
- 【Redis笔记】 第1篇: redis基本概念简介