[leetcode]82. Remove Duplicates from Sorted List

第一题:遍历链表,遇到重复节点就连接到下一个。

public ListNode deleteDuplicates(ListNode head) {
        if (head==null||head.next==null) return head;
        ListNode res = head;
        while (head.next!=null){
            if (head.val==head.next.val)
            {
                if (head.next.next!=null) head.next = head.next.next;
                else head.next = null;
            }
            else head = head.next;
        }
        return res;
    }

第二题:思路比较简单,设置一个超前节点作为head的前节点,往下遍历,遇到重复的就把超前节点连接到新的val节点。

public ListNode deleteDuplicates(ListNode head) {
        if(head==null||head.next==null) return head;
        ListNode res = new ListNode(0);
        res.next = head;
        ListNode list = res;
        while (res.next!=null&&res.next.next!=null)
        {
            ListNode temp = res.next.next;
            if (res.next.val==res.next.next.val)
            {
                while (temp.next!=null&&temp.next.val==temp.val)
                {
                    temp = temp.next;
                }
                if (temp.next!=null) res.next = temp.next;
                else res.next = null;
            }
            else res =res.next;
        }
        return list.next;
    }

当要经常删除第一个节点是,要设置一个超前节点

原文地址:https://www.cnblogs.com/stAr-1/p/8441120.html

时间: 2024-10-16 01:54:50

[leetcode]82. Remove Duplicates from Sorted List的相关文章

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排序链表去重

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. Subscribe to see which companies asked this question 解法:设置两个指

Leetcode 82 Remove Duplicates from Sorted List 2

* Problem: * Given a sorted linked list, delete all nodes that have duplicate numbers * leaving only distinct numbers from the original list. * Solution: * Compare the current position with the previous and behind it. If it does not equal to them, th

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

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

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

[leetcode笔记] 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-&

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

[LeetCode 题解]: 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. 题意:给定一个已排序的链表,删除其中重复的元素,保证每个元素有且仅有一个. 使用两个指针,分别指向前一个元素,和当前元素,