2021/4/13 leetcode83. 删除排序链表中的重复元素

124 篇文章 1 订阅
53 篇文章 0 订阅

在这里插入图片描述
在这里插入图片描述
这个题和剑指上不太一样 剑指上把所有重复元素都删了 这个只保留一个
答案:

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head){
        if(head == nullptr) return nullptr;
        ListNode* pre = head ;ListNode* cur = head->next;
        while(cur != nullptr){
            while(cur != nullptr && pre->val == cur->val){
                cur = cur->next;
            }
            pre->next = cur;
            pre = cur;
        }
        return head;
    }
};

刚开始自己写的:(coding的一些细节还是要注意啊)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head==nullptr) return head;
        ListNode* pre=head;
        ListNode* cur=head->next;
        while(cur!=nullptr){
            while(cur!=nullptr && pre->val==cur->val){
            //不论cur->next是不是nullptr 都可以赋给前一个的指针的next
               pre->next=cur->next;
               cur=pre->next;//要把cur落实 while循环条件里有cur
             }
            pre=cur;
            cur=cur->next;//感觉这里不能写cur的next 因为可能是空的 但是居然连cur->next都不能写
            //pre->next = cur;
            //pre = cur;改成这两句居然就能通过了 无语
           

        }
        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;
    }
}

作者:guanpengchn
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/solution/hua-jie-suan-fa-83-shan-chu-pai-xu-lian-biao-zhong/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

人家怎么写的这么清楚
2021/4/15 解决了一个问题
当时一直不理解为什么单个指针就没有错误 改成两个指针就不行了
单个指针代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
  class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head==nullptr) return nullptr;
        //ListNode* pre=head;
        ListNode* cur=head;
        while(cur!=nullptr&&cur->next!=nullptr){
            if(cur->val==cur->next->val){
                cur->next=cur->next->next;
            }
            else{
                cur=cur->next;
                
            }
        }
        return head;
    }
};

双指针代码

  class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head==nullptr) return nullptr;
        ListNode* pre=head;
        ListNode* cur=head->next;
        while(pre!=nullptr&&cur!=nullptr){
            if(cur->val==pre->val){
                pre->next=cur->next;
				//一定要写下面这句 
 				cur=pre->next;
            }
            else{
                pre=cur;
                cur=cur->next;
                
            }
        }
        return head;
    }
};

双指针要注意迭代的时候两个指针都要变

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值