LeetCode 刷题记录 82. Remove Duplicates from Sorted List II

这篇博客介绍了LeetCode第82题的解决方案,即如何删除有序链表中所有重复的元素,仅保留不重复的数。文中提供了两种解法,一是迭代法,通过增加dummy节点并使用两个指针pre和cur进行遍历和删除操作;二是递归法,通过递归处理链表,遇到重复节点时跳过所有相同节点。
摘要由CSDN通过智能技术生成

题目:
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 2:

Input: 1->1->1->2->3
Output: 2->3
解法1:
迭代法
相比于83题将重复的元素只留一个,该题将重复的元素全部删去
所以head节点也有可能会被删去,所以要增加head节点前增加一个dummy节点
设置两个指针pre和cur,初始指向dummy和head节点
遍历链表,分两种情况,一种是未遇到重复节点,这种情况,每次pre节点被赋为当前节点cur,当前节点cur继续向后移动一位
另一种遇到重复节点,这样的话,一直遍历重复的节点知道最后一个重复的节点,最后通过改变pre指针的位置来忽略这些相同的节点
记得在while循环中加上 head->next不为空的判断条件
c++:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* dummy = new ListNode(-1);
        dummy->next = head;
        ListNode* pre = dummy;
        ListNode* cur = head;
        while(cur && cur->next){
            bool equal = false;
            while(cur->next && cur->val == cur->next->val){
                cur = cur->next;
                equal = true;
            }
            if(equal){
                pre->next = cur->next;
            } else {
                pre = cur;
            }
            cur = cur->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) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode cur = head;
        while(cur!=null && cur.next!=null){
            boolean equal = false;
            while(cur.next!=null && cur.val == cur.next.val){
                cur = cur.next;
                equal = true;
            }
            if(equal){
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
            
        }
        return dummy.next;
    }
}

python:

# 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
        """
        dummy = ListNode(-1)
        dummy.next = head
        pre = dummy
        cur = head
        while cur and cur.next:
            equal = False
            while cur.next and cur.val == cur.next.val:
                cur = cur.next
                equal = True
            
            if equal:
                pre.next = cur.next
            else:
                pre = cur
            
            cur = cur.next
            
        
        return dummy.next

解法2:
递归法
相比于83题将重复的元素只留一个,该题将重复的元素全部删去,所以相比于83题的if条件增加了while循环
83题:如果两个节点相同,保留第二个节点
该题:如果两个节点相同,继续循环向后移动直到到达最后一样相同的节点,然后舍弃这个节点从它的下一个节点继续递归
记得在while循环中加上 head->next不为空的判断条件
c++:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(!head  || !head->next){
            return head;
        }
    
        if(head->val == head->next->val){
            while(head->next && head->val == head->next->val){
                head = head->next;
            }
        
            return deleteDuplicates(head->next);
        }else{
        
            head->next = 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 || head.next == null){
            return head;
        }
    
        if(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;
    }
}

python:

# 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
        """
        if not head  or not head.next:
            return head
        
    
        if head.val == head.next.val:
            while  head.next and head.val == head.next.val:
                head = head.next
        
            return self.deleteDuplicates(head.next)
        else:
        
            head.next = self.deleteDuplicates(head.next)
            
        
        return head
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值