[leedcode 82] 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.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    //本题自我感觉很难,主要突破点:
    //1:注意要声明一个节点p,该节点的前面代表已经满足要求的
    //s节点代表重复值得第一位,通过遍历t比较t指向的值是否等于s,当相等时,这里很重要,通过比较s.next==t可知,是否有重复变量,
    //如果有重复变量,直接使得p指向t,如果没有重复遍历,需要保存s的节点
    //最后需要考虑s是否已经遍历到最后一位,如果s没有遍历到最后一位,而t遍历到了最后,说明s到t中间有重复值,即s要舍弃,所以p.next=null
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null||head.next==null) return head;
        ListNode newHead=new ListNode(-1);
        newHead.next=head;
        ListNode p=newHead;
        ListNode s=head;
        ListNode t=head.next;
        while(t!=null){
            if(t.val==s.val){
                t=t.next;
            }else{
                if(s.next==t){
                    //p.next=s;
                    p=s;
                    s=t;
                    t=t.next;
                }else{
                    p.next=t;
                    s=t;
                    t=t.next;

                }
            }

        }
        if(s.next!=null){
            p.next=null;
        }
        return newHead.next;
    }
}
时间: 2024-10-05 23:37:26

[leedcode 82] Remove Duplicates from Sorted List II的相关文章

82. Remove Duplicates from Sorted List II && i

题目 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. Given 1->1->2->3->3, return 1->2->3. 解析 [LeetCode] Rem

82. Remove Duplicates from Sorted List II(js)

82. 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. Example 1: Input: 1->2->3->3->4->4->5 Output: 1->2->5 Example

LeetCode(82): 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->

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

转载请注明出处: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

82. 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.

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

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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-&

82. Remove Duplicates from Sorted List II (List)

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

LeetCode OJ 82. 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.

leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的反复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