题目:删除排序链表中的重复数字 II
来源:互联网 发布:吉永小百合 长相 知乎 编辑:程序博客网 时间:2024/06/05 10:41
给定一个排序链表,删除所有重复的元素只留下原链表中没有重复的元素。您在真实的面试中是否遇到过这个题?
Yes
样例
给出1->2->3->3->4->4->5->null,返回1->2->5->null
给出1->1->1->2->3->null,返回 2->3->null
标签 Expand
解题思路:
当前节点cur.val若是cur.next.val的节点相同,则把遍历把与cur.val相等的节点都找到,并且跳过。若是当前节点cur.val若是cur.next.val的节点不相同,则将该节点放入res中。
/*** Definition for ListNode* public class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/public class Solution { /** * @param ListNode head is the head of the linked list * @return: ListNode head of the linked list */ public static ListNode deleteDuplicates(ListNode head) { // write your code if (null == head) return null; ListNode res = new ListNode(0); ListNode pre = res; ListNode cur = head; while (cur != null) { if (cur.next!=null&&cur.val == cur.next.val) { while (cur.next!=null&&cur.val == cur.next.val) { cur = cur.next; } }else{ pre.next = cur; pre = pre.next; } cur = cur.next; } //处理最后一个节点 pre.next = cur; return res.next; }}
0 0
- 题目:删除排序链表中的重复数字 II
- 题目:删除排序数组中的重复数字 II
- LintCode-删除排序链表中的重复数字 II
- 删除排序链表中的重复数字 II
- lintcode-删除排序链表中的重复数字II-113
- 删除排序链表中的重复数字 II
- 删除排序链表中的重复数字 II
- 删除排序链表中的重复数字 II
- lintcode-删除排序链表中的重复数字 II
- LintCode(M)删除排序链表中的重复数字 II
- lintcode,删除排序链表中的重复数字 II
- LintCode 删除排序链表中的重复数字 II
- 删除排序链表中的重复数字 II -LintCode
- 删除排序链表中的重复数字 II -LintCode
- [LintCode]113.删除排序链表中的重复数字 II
- LintCode删除排序链表中的重复数字 II
- 删除排序数组中的重复数字 II
- 删除排序数组中的重复数字 II
- Effective C++——条款14(第3章)
- 堆,栈,堆栈
- Commview for wifi的尝试
- [Cocoa]_[获取系统时间]
- 生活随笔:技术转型
- 题目:删除排序链表中的重复数字 II
- 题目:删除数字
- WebMVC中EasyUI的ComboBox控件的运用
- 算法竞赛入门经典(第二版) 例题4-4 信息编码(Message Decoding) UVa213 Finals1991 (二进制)
- CSS做一个小黄人
- linux C sqlite3 mysql
- 题目:前序遍历和中序遍历树构造二叉树
- 多线程渲染(Multithreaded- rendering)3D引擎实例分析 : FlagshipEngine
- C#编码标准和命名规范