LeetCode 刷题记录 83. Remove Duplicates from Sorted List

该博客记录了LeetCode第83题的解决方案,即如何从已排序的链表中删除所有重复的元素,使得每个元素只出现一次。博主提供了两种方法,一种是迭代法,通过遍历链表并比较相邻节点来实现;另一种是递归法,通过设置递归基并比较当前节点与下一个节点的值来决定是否删除当前节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:
Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2
Example 2:

Input: 1->1->2->3->3
Output: 1->2->3
解法1:
迭代法:
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* cur = head;
        while(cur && cur->next){
            if(cur->val == cur->next->val){
                cur->next = cur->next->next;
            } else {
                cur = cur->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) {
        ListNode cur = head;
        while(cur!=null && cur.next!=null){ 
        
            if(cur.val == cur.next.val){ 
                cur.next = cur.next.next; 
       
            }else{
                cur = cur.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
        """
        cur = head
        while cur and cur.next: 
        
            if cur.val == cur.next.val:
                cur.next = cur.next.next 
       
            else:
                cur = cur.next
            
        
        return head

解法2:递归:
递归基:
如果只有一个结点或者没有结点,则返回
判断当前结点和它的下一个结点的值,如果相等,则舍弃当前结点,继续递归它的下一个结点
如果不等,继续递归它的下一个结点,但是要通过指针操作将当前结点与下一个结点接上,并返回当前结点

/**
 * 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){
                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){
        
            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:
        
            return self.deleteDuplicates(head.next)
        else:
        
            head.next = self.deleteDuplicates(head.next)
            
        
        return head
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值