关闭

【Leetcode】Remove Duplicates from Sorted List II

430人阅读 评论(0) 收藏 举报
分类:

题目链接: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 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

思路:

思路很简单,唯一要注意的是 删除操作加入头结点可以减少很多判断。

算法:

	public ListNode deleteDuplicates(ListNode head) {
		if(head==null)
			return null;
		ListNode newHead = new ListNode(0);
		newHead.next = head;
		ListNode p = head, q, pre = newHead;
		// pre p q
		q = p.next;
		while (q != null) {
			if (p.val == q.val) {
				while (q != null && p.val == q.val) {// 如果节点相等则前移至后面第一个不等节点
					q = q.next;
				}
				pre.next = q;
				p = pre;//记得把p要更新,否则p指向的是要删除的结点,下次循环进入else会出错。p不能更新为q,否则下轮循环p/q相等进入死循环。
			} else {
				pre = p;
				p = q;
				q = q.next;
			}
		}
		return newHead.next;
	}

算法2:没加入头结点,简直超麻烦,判断边界条件神烦啊。。自从删除、增加结点操作加入头结点之后,代码简洁可读性强多了。亲身体会!


	public ListNode deleteDuplicates(ListNode head) {
		ListNode p = head, q, pre = null;
		if (p == null) {
			return head;
		}
		// pre p q
		q = p.next;
		while (q != null) {
			if (p.val == q.val) {
				while (q != null && p.val == q.val) {// 如果节点相等则前移至第一个不等节点
					q = q.next;
				}
				if (pre == null) {// 如果头节点重复,需要特殊处理
					head = q;// 如此时1,1,2,2
					p = q;// head、p都指向2
					if (q != null) // 若不是1,1这种情况,即q为null时,q向前移到第二个2位置
						q = q.next;
				} else { //
					pre.next = q;
					p = pre;
				}
			} else {
				pre = p;
				p = q;
				q = q.next;
			}
		}
		return head;
	}




0
0
查看评论

leetcode:82. Remove Duplicates from Sorted List II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705 题目地址:https://leetcode.com/problems/reverse-linked-list-ii/ Remove Duplicates from Sorted...
  • u012975705
  • u012975705
  • 2015-12-27 00:18
  • 1366

Remove Duplicates from Sorted List -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/  这是一道比较简单的链表操作的题目,要求是删去有序链表中重复的元素。方法比较清晰,维护两个指针,一个指向当前不重复的最后一个元素,...
  • linhuanmars
  • linhuanmars
  • 2014-04-23 11:25
  • 8717

【LeetCode with Python】 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 1-...
  • nerv3x3
  • nerv3x3
  • 2014-08-29 18:36
  • 2685

Remove Duplicates from Sorted List II -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/  这道题跟Remove Duplicates from Sorted List比较类似,只是这里要把出现重复的元素全部删除。...
  • linhuanmars
  • linhuanmars
  • 2014-04-24 08:33
  • 10648

82. Remove Duplicates from Sorted List II Leetcode Python

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, ...
  • hyperbolechi
  • hyperbolechi
  • 2015-02-05 11:48
  • 947

Remove Duplicates from Sorted Array II -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates from Sorted Array比较类似,区别只是这里元素可以重复出现...
  • linhuanmars
  • linhuanmars
  • 2014-04-23 08:39
  • 10686

LeetCode_Remove Duplicates from Sorted List II

LeetCode_Remove Duplicates from Sorted List II
  • shengno1
  • shengno1
  • 2015-04-15 00:09
  • 1107

Leetcode_83_Remove Duplicates from Sorted List

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41728739 Given a sorted linked list, delete all duplicates such that each elemen...
  • pistolove
  • pistolove
  • 2014-12-04 19:20
  • 1503

[LeetCode]80.Remove Duplicates from Sorted Array II

【题目】 Remove Duplicates from Sorted Array II  Total Accepted: 4460 Total Submissions: 15040My Submissions Follow up for &qu...
  • SunnyYoona
  • SunnyYoona
  • 2014-01-15 09:51
  • 1749

LeetCode(83)Remove Duplicates from Sorted List

题目如下: Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Giv...
  • feliciafay
  • feliciafay
  • 2014-01-10 08:06
  • 2712
    个人资料
    • 访问:568062次
    • 积分:8667
    • 等级:
    • 排名:第2676名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论