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

原创 2015年12月27日 00:18:19

转载请注明出处:z_zhaojun的博客
原文地址:http://blog.csdn.net/u012975705/article/details/50411033
题目地址:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

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.

解法(Java):

/**
 * 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 helper = new ListNode(0);
        ListNode pre = helper;
        boolean isDup = false;
        while (head != null && head.next != null) {
            if (head.val == head.next.val) {
                isDup = true;
            } else if (isDup) {
                isDup = false;
            } else {
                pre.next = head;
                pre = pre.next;
            }
            head = head.next;
        }
        if (!isDup) {
            pre.next = head;
            pre = pre.next;
        } else {
            pre.next = null;
        }
        return helper.next;
    }
}
版权声明:本文为博主原创文章,转载请注明出处。博客首页:http://blog.csdn.net/u012975705。订阅:http://blog.csdn.net/u012975705/rss/list

leetcode-java-22. Generate Parentheses

本题明显用递归: 递归就是把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解 本题就是用递归,也就是判断每个字符应该是(还是) left>0时—-字符串中添加字符( righ...

LeetCode 82之Remove Duplicates from Sorted List II的Java题解

题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dist...

(Leetcode 82) Remove Duplicates from Sorted List II (Java)

题目:Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have...

[LeetCode-82] Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb...

leetcode 82. Remove Duplicates from Sorted List II (线性表)

leetcode 82. Remove Duplicates from Sorted List IIGiven a sorted linked list, delete all nodes that ...

LeetCode82:Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct num...

【一天一道LeetCode】#82. Remove Duplicates from Sorted List II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目...

LeetCode - 82. Remove Duplicates from Sorted List II

这一道题目主要是链表的操作,一开始因为情况比较复杂,把自己绕晕了,所以没想到这道题目的解法,后来看了答案才觉得自己解起来确实很难解出。这道题目对于链表的操作比较复杂,所要考虑的情况也很多,最大的一个技...

leetcode_82_Remove Duplicates from Sorted List II

思路: 大致思路就是,遍历链表找出重复元素的子列并删除重复元素子列,当然,第一个元素开始有重复元素的话比较特种,需要特殊考虑。删除子列的过程稍微有点绕,题目倒是不难理解。...

[leetcode 82]Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:82. Remove Duplicates from Sorted List II(Java)解答
举报原因:
原因补充:

(最多只允许输入30个字)