zoukankan      html  css  js  c++  java
  • [LeetCode] 82. 删除排序链表中的重复元素 II

    题目链接 : https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/

    题目描述:

    给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。

    示例:

    示例 1:

    输入: 1->2->3->3->4->4->5
    输出: 1->2->5
    

    示例 2:

    输入: 1->1->1->2->3
    输出: 2->3
    

    思路:

    思路一: 迭代 快慢指针,用快指针跳过那些有重复数组,慢指针负责和快指针拼接!

    思路二:递归

    相关题目:83. 删除排序链表中的重复元素

    代码:

    思路一:

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def deleteDuplicates(self, head: ListNode) -> ListNode:
            if head == None or head.next == None:
                return head
            dummy = ListNode(-1000)
            dummy.next = head
            slow = dummy
            fast = dummy.next
            while fast:
                if  fast.next and fast.next.val == fast.val:
                    tmp = fast.val
                    while fast and tmp == fast.val:
                        fast = fast.next
                else:
                    slow.next = fast
                    slow = fast
                    fast = fast.next
            slow.next = fast
            return dummy.next
    

    思路一(另一个版本):

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None 
    class Solution:
        def deleteDuplicates(self, head):
            # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def deleteDuplicates(self, head: ListNode) -> ListNode:
            if head == None or head.next == None:
                return head
            dummy = ListNode(-1)
            dummy.next = head
            slow = dummy
            fast = dummy.next
            while fast:
                while fast.next and slow.next.val == fast.next.val:
                    fast = fast.next
                if slow.next == fast:
                    slow = fast
                else:
                    slow.next = fast.next
                fast = fast.next
            return dummy.next
    

    java

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if (head == null) return head;
            ListNode dummy = new ListNode(-1000);
            dummy.next = head;
            ListNode slow = dummy;
            ListNode fast = dummy.next;
            while (fast != null) {
                while (fast.next != null && fast.val == fast.next.val) fast = fast.next;
                if (slow.next == fast) slow = slow.next;
                else slow.next = fast.next;
                fast = fast.next;
            }
            return dummy.next; 
        }
    }
    

    思路二:

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def deleteDuplicates(self, head: ListNode) -> ListNode:
            if not head:return head
            if head.next and head.val == head.next.val:
                while head.next != None and head.val == head.next.val:
                    head = head.next
                return self.deleteDuplicates(head.next)
            else:
                head.next = self.deleteDuplicates(head.next)
            return head
            
    

    java

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if (head == null)  return head;
            if (head.next != null && head.val == head.next.val) {
                while (head.next != null && head.val == head.next.val) {
                    head = head.next;
                }
                return deleteDuplicates(head.next);
            }
            else head.next = deleteDuplicates(head.next);
            return head;    
        }
    }
    
  • 相关阅读:
    java泛型的一些知识点:Java泛型--泛型应用--泛型接口、泛型方法、泛型数组、泛型嵌套
    Java遍历Map的四种方式
    Less20、21、22【报错注入+Cookie字段注入+Base64编码】
    Less18、19【报错注入+User-Agent、Referer字段注入】
    Less17【报错注入+Update注入/时间注入】
    Less-16【盲注+时间注入】
    Less-15【盲注+时间注入】
    Less-14【报错注入】
    Less-12、13【报错注入】
    Less-11【报错注入】
  • 原文地址:https://www.cnblogs.com/powercai/p/10980082.html
Copyright © 2011-2022 走看看