java LinkedList,leetcode #83,总是满足条件[已关闭]

uz75evzq  于 2023-05-12  发布在  Java
关注(0)|答案(2)|浏览(96)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
2天前关闭。
Improve this question
为什么这个代码不工作?if(head!= head.next)和(head.瓦尔!= head.next.瓦尔)在100%的情况下都有效。但我在答案中发现了一个对人有效的相同条件的代码。请帮我找出错误。
LeetCode:https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {

        ListNode trueResult = new ListNode(-1);
        ListNode result = trueResult;

        while (head != null) {
                
                if (head != head.next) {
                    result.next = head;
                    head = head.next;
                }

                result = result.next;
        }

        return trueResult.next;
    }
}

结果:head = [1,1,2,3,3] output = [1,1,2,3,3] expected = [1,2,3]
这个代码工作。为什么?

* Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        
        ListNode temp = head;

        while( temp != null && temp.next != null ){

            if( temp.val == temp.next.val ){
                temp.next = temp.next.next;
            }
            else{
                temp = temp.next;
            }

        }

        return head;

    }
}

if(head!= head.next)和if(head.瓦尔!= head.next.瓦尔)工作到100%情况。每次我都是一个糟糕的结果。
我尝试使用头。瓦尔!= head.next.瓦尔,但它不工作:(

ryevplcw

ryevplcw1#

你的代码中有一些问题。
1.参考文献比较

if (head != head.next)

当比较references时,此条件始终为true。您应该比较ListNode#val

**a.**将校验替换为head.val != head.next.val。注意null检查head.next
**B.**如果head.next为null,则应始终使用if语句,因为这是最后一个元素

1.您始终将result提前,而不将head提前
即使你遇到了重复你提前的结果,这是没有意义的。如果我们发现一个重复,我们不应该前进result,因为我们没有修改列表。

**a.**每次迭代都需要推进head,而不是推进结果,即if语句外的模式head = head.next
**B.**只有在没有重复的情况下才需要提前结果,即move result前进到if语句

我将所有的亮点应用到你的代码中,你可以在下面看到:

class Solution {
    public ListNode deleteDuplicates(ListNode head) {

        ListNode trueResult = new ListNode(-1);
        ListNode result = trueResult;

        while (head != null) {
                
                if (head.next == null || (head.next != null && head.val != head.next.val)) {
                    result.next = head;
                    result = result.next;
                }

                head = head.next;
        }

        return trueResult.next;
    }
}

编码快乐!

rur96b6h

rur96b6h2#

他们似乎想要这个(像):

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode cur = head; // "cursor" head to tail
        while (cur != null) {
            ListNode del = cur.next; // cursor over "deletion candidates"
            while (del != null && del.val <= cur.val) { // del.val <= cur.val means it is not sorted or equal! del == null means we are done
                del = del.next; // just iterate (inner loop)..
            }
            // when terminated, we are good!:
            // del == null || del.val > cur.val  (is now true!;)
            cur.next = del; // re-link
            cur = del; // iterate (outer loop)...
        } // outer done, cur == null (== "tail")
        return head; // return the original head (with (eventually) less "links"/nodes) ..all "forgotten nodes" are ready for GC
    }
}

已接受

运行时间0毫秒心跳100%

内存42.7MB,心跳7.62%

相关问题