Remove Duplicates from Sorted List
来源:互联网 发布:手机虚拟局域网软件 编辑:程序博客网 时间:2024/05/23 01:17
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
.
题意:输入一个有序链表,删除重复元素,确保每个值只出现一次
思路:使用两个指针
/** * 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) {return head;} ListNode preNode = head;//保存非重复元素的列表 ListNode curNode = head.next; while (curNode != null) { <span style="white-space:pre"></span>//如果值不同,preNode节点向后移动 if (curNode.val != preNode.val) {preNode = preNode.next;} curNode = curNode.next;//curNode向后移动 preNode.next = curNode;//preNode节点指向新的curNode节点 } return head; }}
下面是最开始使用的方法,其实下面的ifelse语句可以简化成上面的方法
public ListNode deleteDuplicates(ListNode head) {if (head == null) {return head;} ListNode preNode = head; ListNode curNode = head.next; while (curNode != null) { <span style="white-space:pre"></span>//值相等时,移动curNode,然后再使preNode指向新的curNodeif (preNode.val == curNode.val) {curNode = curNode.next;preNode.next = curNode;}else {//值不等时,preNode和curNode同时向后移动,然后preNode再指向新的curNodepreNode = preNode.next;curNode = curNode.next;preNode.next = curNode;} } return head;}
下面是这个题的拓展题目:
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5。
解题思路:(1)链表头可能会被删除。所以使用dummy节点,这样让操作更直观
(2)如果当前元素没有重复,则加入到结果链表里。
判断元素是否重复:使用一个指针记录当前元素第一次出现的节点,然后第二个指针遍历具有相同元素的节点,遍历玩之后,如果第一个指针和第二个指针指向同一个节点。则意味着该节点没有重复,因为元素链表是有序的,拥有相同元素的节点是紧挨着的
public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode deleteDuplication(ListNode pHead) { if(pHead == null) return null; ListNode dummy = new ListNode(0); ListNode tail = dummy;//用来保存结果 ListNode preNode = pHead; ListNode curNode = pHead; while(curNode != null && curNode.next != null){ //移动当前指针到重复元素的最后一个 while(curNode .next != null && curNode.val == curNode.next.val){ curNode = curNode.next; } if(preNode == curNode){ //如果当前元素没有重复的,那么加入当前元素 tail.next = preNode; tail = tail.next; } preNode = curNode.next; curNode = curNode.next; } tail.next = curNode;//加入最后一个元素 return dummy.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
- 【Codeforces Round #297 (Div. 2)】Codeforces 525E Anya and Cubes
- 微信支付流程及集成教程
- ubuntu安装teamviewer
- Lae程序员小漫画(1),仅供一乐
- Hash算法冲突解决方法分析
- Remove Duplicates from Sorted List
- 基于s5pv210处理器驱动相关整理
- 进程间通信之消息队列
- 软件程序编写规范 - 上(仅供参考)
- MySQL 语句大全:创建、授权、查询、修改等
- LightOJ1215 Finding LCM
- bzoj 3065 带插入区间K小值
- Windows中的SAM文件
- LNMP1.3 phpMyAdmin 打开空白的问题