【问题标题】:OddEven Linked List code doesnt work on leetcode奇偶链表代码在 leetcode 上不起作用
【发布时间】:2016-10-16 17:11:26
【问题描述】:

leetcode 上有个问题叫奇偶链表。

上面写着:

给定一个单链表,将所有奇数节点组合在一起,然后是偶数节点。请注意,这里我们讨论的是节点编号,而不是节点中的值。

您应该尝试在适当的位置进行操作。程序应该以 O(1) 的空间复杂度和 O(nodes) 的时间复杂度运行。

示例: 给定 1->2->3->4->5->NULL, 返回 1->3->5->2->4->NULL。

这是我的节点类

public class Node 
{
private int value;
private Node next;

public Node(int Value) 
{
    this.value = Value;
    this.next = null;
}

public Node()
{
    this.value = -1;
    this.next = null;
}

public Node getNext() {
    return next;
}public void setNext(Node next) {
    this.next = next;
}public int getValue() {
    return value;
}public void setValue(int value) {
    this.value = value;
}
}

我在列表中有 8 个项目,其值为 1、2、3、4、5、6、7、8。这是我的输出-->1-->3-->5-->7-->2-->4-->6-->8 这是我解决 OddEven 任务的链表方法。

public void oddEven()
{
    if(head.getNext() == null)
        return;

    Node lastOdd = head.getNext(); // gets the value of last odd even in list.
    Node current = lastOdd.getNext(); // Puts the reference on the first even index.
    Node before = lastOdd; // This node, will always be one index before current Node
    int travel = 1, loop;


    while(current != null)
    {
        loop = travel;
        // Prvo petlja putuje do sledeceg neparnog elementa
        while(loop-- > 0)
        {
            before = current;
            current = current.getNext();
        }

        if(current == null) // If it is end of the list, exit loop.
            break;

        before.setNext(current.getNext());
        current.setNext(lastOdd.getNext());
        lastOdd.setNext(current);
        lastOdd = current;
        current = before.getNext();

    }

}

它在我的电脑上运行良好。但是当我将代码放入 leetcode 时,我得到了它不起作用的错误。但它是相同的代码。这是leetcode的代码

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

    if(head.next == null)
        return head;

    ListNode lastOdd = head.next; // gets the value of last odd even in list.
    ListNode current = lastOdd.next; // Puts the reference on the first even index
    ListNode before = lastOdd;
    int travel = 1, loop;


    while(current != null)
    {
        loop = travel;
        // Prvo petlja putuje do sledeceg neparnog elementa
        while(loop-- > 0)
        {
            before = current;
            current = current.next;
        }

        if(current == null)
            break;



        before.next = current.next;
        current.next = lastOdd.next;
        lastOdd.next = current;
        lastOdd = current;
        current = before.next;

    }
    return head;
}
}

这是我得到的错误

对于输入:[1,2,3,4,5,6,7,8]

你的答案:[1,2,4,6,8,3,5,7]

预期答案:[1,3,5,7,2,4,6,8]

但都是同样的方法,我哪里弄错了?

【问题讨论】:

  • if(head.next == null) 不检查 (head != null) 是错误的。

标签: java algorithm linked-list


【解决方案1】:

我去了那个网站,把你的代码放进去,以便更容易看到发生了什么,问题是这一行:

ListNode lastOdd = head.next; // gets the value of last odd even in list.

列表的第一个元素是 1,但您将最后一个奇数作为列表中的下一个元素开始,而不是列表的头部。只需将其更改为:

ListNode lastOdd = head

它提供了正确的答案。

代码本身需要整理一下,内部的 while 循环似乎没有真正的用途,不知道为什么会在那里,或者它是之前尝试的遗留元素?

【讨论】:

  • 没关系,我的功能是无效的。那个return是退出函数的方式,它们的函数是ListNode类型的,所以返回的是空头。是一样的。
【解决方案2】:

looptravel 变量在做什么?它们在每次迭代中都恒定为 1,那么为什么是 while(loop-- > 0) 循环。抱歉,没有得到您想要通过该循环实现的目标。

int travel = 1, loop;
    while(current != null)
    {
        loop = travel;
        // Prvo petlja putuje do sledeceg neparnog elementa
        while(loop-- > 0)
        {

解决方案:查找列表中的最后一个节点。通过追加将所有偶数定位的节点移动到末尾。确保在第一步中标记为最后一个节点处停止,否则它将运行到 infinite 循环。

【讨论】:

    猜你喜欢
    • 2014-05-04
    • 2018-10-25
    • 2021-06-29
    • 1970-01-01
    • 2012-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多