查看: 1058| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[树/链表/图] 咨询一道链表去重问题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
题目在这里82. Remove Duplicates from Sorted List II我觉得这个和string去重思路是一样的,重复的一个也不留,这个是string的写法:
  1. public String deDup(String input) {

  2.    //corner case

  3.    if(input==null || input.length()<=1) return input;
  4.    char[] array = input.toCharArray();
  5.    int end=0; //the top of stack
  6.    for(int i=1;i<array.length;i++){
  7.         if(end == -1 || array[i]!=array[end]){
  8.             array[++end] = array[i]; //日常拷贝
  9.         }else{
  10.             end--;
  11.             while(i+1<array.length && array[i]==array[i+1]){
  12.                 i++;
  13.             }
  14.        }
  15.     }
  16.     return new String(array,0,end+1);
  17. }
复制代码


所以我觉得相同的思路,这个链表也能写,所以就有了:
  1. public ListNode deleteDuplicates(ListNode head) {
  2.         if(head == null) return head;
  3.         ListNode slow = head;
  4.         ListNode fast = head;
  5.         //here should be dummy head
  6.         ListNode dummy = new ListNode(-1);
  7.         dummy.next = head;
  8.         // here should have dummy && pre node of slow
  9.         ListNode prev = head;
  10.       
  11.         while(fast!= null){
  12.             if(slow.val != fast.val){
  13.                 prev.val = slow.val;
  14.                 slow = slow.next;
  15.                 slow.val = fast.val;

  16.             }else{
  17.                
  18.                 slow = prev; //死循环,而且还不能任意改动.val值
  19.                 while(fast.next!=null && slow.val == fast.val){
  20.                     fast = fast.next;
  21.                 }
  22.             }
  23.         }
  24.         //断点
  25.         slow.next = null;
  26.         return dummy.next;
  27.     }
复制代码


可是我发现,换成链表了要不就是slow!=null || 不能加,要么就会陷入死循环
不知道该如何处理prev(对应end--)这个操作
在list中引入prev,这样写会在出现NPE      
  1. if(slow!=null|| slow.val != fast.val){
  2.                 prev = slow;
  3.                 slow = slow.next;
  4.                 slow.val = fast.val; //here
复制代码

然后这样改动还会死循环:

  1. }else{         
  2.                 slow = prev; //死循环,因为slow永远=prev
  3.                 while(fast.next!=null && fast.val == fast.next.val){
  4.                     fast = fast.next;
  5.                 }
复制代码

想了一天,问了一天了,实在没办法po在地里了,希望大佬们指点迷津
就是按照string那个思路来走,对应着链表中,应该怎么改呢

上一篇:2020.4.13 LeetCode公司tag
下一篇:开帖刷题 🤑
🔗
usr_opta 2020-4-21 07:51:59 | 只看该作者
全局:
我觉得你可以尝试先理清你的slow/fast/prev三个变量分别代表什么。
你一开始就把它们全指向了head,这种情况下你的dummy的意义是什么?
回复

使用道具 举报

🔗
usr_opta 2020-4-21 08:00:29 | 只看该作者
全局:
附上我的solution以供参考
  1. class Solution {
  2.     public ListNode deleteDuplicates(ListNode head) {
  3.         if (head == null) return null;
  4.         ListNode pseudo = new ListNode(0);
  5.         pseudo.next = head;
  6.         
  7.         ListNode taken = pseudo;
  8.         ListNode begin_seg = head;
  9.         ListNode curr = head.next;
  10.         
  11.         while(true) {
  12.             boolean dup = false;
  13.             while (curr != null && begin_seg.val == curr.val) {
  14.                 curr = curr.next;
  15.                 dup = true;
  16.             }
  17.             if (dup) {
  18.                 taken.next = curr;
  19.             } else {
  20.                 taken = begin_seg;
  21.             }
  22.             if (curr == null) {
  23.                 return pseudo.next;
  24.             } else {
  25.                 begin_seg = curr;
  26.                 curr = curr.next;
  27.             }
  28.         }
  29.     }
  30. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表