LeetCode 82. Remove Duplicates from Sorted List II(删除链表中的重复节点)
2016-05-22 05:32
615 查看
原题网址:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given
Given
方法:直接处理,注意引入辅助头节点是个好办法。
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given
1->2->3->3->4->4->5, return
1->2->5.
Given
1->1->1->2->3, return
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) { ListNode start = new ListNode(0); start.next = head; ListNode current = start; while (current.next != null && current.next.next != null) { if (current.next.val == current.next.next.val) { ListNode duplicate = current.next.next.next; while (duplicate != null && duplicate.val == current.next.val) { duplicate = duplicate.next; } current.next = duplicate; } else { current = current.next; } } return start.next; } }
相关文章推荐
- LeetCode 81. Search in Rotated Sorted Array II(搜索旋转的数组)
- LeetCode 80. Remove Duplicates from Sorted Array II(删除重复)
- Python64位安装模块问题
- LeetCode 79. Word Search(单词查找)
- LeetCode 77. Combinations(组合)
- LeetCode 78. Subsets(子集)
- LeetCode 74. Search a 2D Matrix(搜索矩阵)
- LeetCode 75. Sort Colors(颜色排序)
- LeetCode 76. Minimum Window Substring(最小窗口子串)
- LeetCode 72. Edit Distance(编辑距离)
- LeetCode 73. Set Matrix Zeroes(矩阵清零)
- 输入输出外挂总结
- leetcode #22 in cpp
- 关于Gson.jar导入正常使用,代码无错,但程序执行解析时崩溃的一些问题
- [PWA] 18. Clean the photo cache
- leetcode #21 in cpp
- mathematica试除法解素数问题
- 全局eval的实现
- 法国劳动法案引大罢工 一场“无解”的左翼运动
- [PWA] 17. Cache the photo