82. 删除排序链表中的重复元素II

https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-15 16:48:30
 * @LastEditTime: 2020-08-15 17:33:33
 * @Description: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/
 * @FilePath: \leetcode-googtech\#82. Remove Duplicates from Sorted List II\Solution.java
 * @Reference: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/solution/cai-niao-di-yi-ci-xie-ti-jie-zhi-shi-dui-jie-ti-gu/
 * @WebSite: https://algorithm.show/
 */

/*
 * @lc app=leetcode.cn id=82 lang=java
 *
 * [82] 删除排序链表中的重复元素 II
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        // 初始化辅助节点
        ListNode listNode = new ListNode(0);
        listNode.next = head;
        // 初始化辅助节点指针
        ListNode currentNode = listNode;
        // 通过移动当前指针 currrentNode 来循环遍历链表
        while(currentNode.next != null && currentNode.next.next != null) {
            // 判断当前节点的下一个节点(真实链表的头节点)的值与下下个节点的值是否相同
            if(currentNode.next.val == currentNode.next.next.val) {
                // 初始化待删节点,即将重复的节点视为单独的一个链表
                // 有重复就将指向待删节点的指针往后移,直到指向待删链表的最后一个节点
                ListNode tempNode = currentNode.next;
                while(tempNode != null && tempNode.next != null && tempNode.val == tempNode.next.val) {
                    tempNode = tempNode.next;
                }
                // 将待删接节点的前节点指针指向 tempNode 的后一个节点,即可实现删除所有重复节点
                currentNode.next = tempNode.next;
            }else {
                // 继续移动当前节点指针,进入下一个循环来继续判断链表中是否有相邻重复的节点
                currentNode = currentNode.next;
            }
        }
        // 返回真实链表的头节点
        return listNode.next;
    }
}
// @lc code=end

Python

'''
Author: Goog Tech
Date: 2020-08-15 17:13:45
LastEditTime: 2020-08-15 17:33:03
Description: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/
FilePath: \leetcode-googtech\#82. Remove Duplicates from Sorted List II\Solution.py
Reference: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/solution/cai-niao-di-yi-ci-xie-ti-jie-zhi-shi-dui-jie-ti-gu/
WebSite: https://algorithm.show/
'''

#
# @lc app=leetcode.cn id=82 lang=python
#
# [82] 删除排序链表中的重复元素 II
#

# @lc code=start
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        # 初始化辅助节点
        listNode = ListNode()
        listNode.next = head
        # 初始化辅助节点指针
        currentNode = listNode
        # 通过移动当前节点指针 currentNode 来循环遍历链表
        while currentNode.next is not None and currentNode.next.next is not None:
            # 判断当前节点的下一个节点(真实链表的头节点)的值与下下个节点的值是否相同
            if currentNode.next.val == currentNode.next.next.val:
                # 初始化待删节点,即将重复的节点视为单独的一个链表
                # 有重复就将指向待删节点的指针往后移,直到指向待删链表的最后一个节点
                tempNode = currentNode.next
                while tempNode is not None and tempNode.next is not None and tempNode.val == tempNode.next.val:
                    tempNode = tempNode.next
                # 将待删节点的前节点指针指向 tempNode 的后一个节点,即可实现删除所有重复节点
                currentNode.next = tempNode.next
            # 继续移动当前节点指针,进入下一个循环来继续判断链表中是否有相邻重复的节点
            else:
                currentNode = currentNode.next
        return listNode.next
# @lc code=end
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""