不构成链接的链表实现(java)

不构成链接的链表实现(java),java,linked-list,Java,Linked List,我正在尝试一个leetcode问题,我需要在java中实现一个链表,但是“链接”永远不会被创建。节点本身确实会被创建,但会丢失在内存中。我知道如何在C++中使用指针来实现这一点,但是这在java中如何工作? 问题是: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 印刷品: 7 0 8 返回: 7 (just head node) 我的代码: /** * Definition for s

我正在尝试一个leetcode问题,我需要在java中实现一个链表,但是“链接”永远不会被创建。节点本身确实会被创建,但会丢失在内存中。我知道如何在C++中使用指针来实现这一点,但是这在java中如何工作? 问题是:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
印刷品:

7
0
8
返回:

7 (just head node)
我的代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        //hold root node to return later, use temp node (l3) to create list
        ListNode head = new ListNode(0);
        ListNode l3 = head;

        boolean carryover = false;

        //if lists l1, l2 still have a value, append to l3
        while (l1 != null || l2 != null)
        {
            //always true except on first iteration
            if (l3 == null)
                l3 = new ListNode(0);

            //if l1.val + l2.val >= 10 from last iteration, carry over 1
            if (carryover)
            {
                l3.val += 1;
                carryover = false;
            }

            if (l1 != null)
            {
                l3.val += l1.val;
                l1 = l1.next;
            }

            if (l2 != null)
            {
                l3.val += l2.val;
                l2 = l2.next;
            }

            if (l3.val > 9)
            {
                l3.val -= 10;
                carryover = true;
            }
            System.out.println(l3.val);

            //create next 'link' in list
            l3 = l3.next;
        }
        return head;
    }
}

对于合并两个列表,每个列表由第一个节点(头)定义


l3=l3.next没有做您认为正在做的事情

l3。下一个
null
,因此您将
null
分配给
l3
null
不是由
l3指向的内存中的特殊位置。接下来,它只是
null
,这意味着它没有指向任何东西

所以在下一个循环中,当您执行
l3=newlistnode(0)时,您只是在创建一个断开连接的节点

您应该首先确保
next
指向一个节点,然后才能使用它

因此,请尝试以下方法:

boolean first = true;

//if lists l1, l2 still have a value, append to l3
while (l1 != null || l2 != null)
{
    // create the next node
    if (!first) {
        // create the next node and attach it to the current node
        l3.next = new ListNode(0);
        // we now work with the next node
        l3 = l3.next;
    } else {
        first = false;
    }

    //if l1.val + l2.val >= 10 from last iteration, carry over 1
    if (carryover)
    {
        l3.val += 1;
        carryover = false;
    }

    if (l1 != null)
    {
        l3.val += l1.val;
        l1 = l1.next;
    }

    if (l2 != null)
    {
        l3.val += l2.val;
        l2 = l2.next;
    }

    if (l3.val > 9)
    {
        l3.val -= 10;
        carryover = true;
    }
    System.out.println(l3.val);

}

通常每个
节点
都有一个变量,称为
next
next
变量的类型为
Node
。如果您想向列表中添加一个新节点,该方法将创建一个新的
节点
,并使用前面的
tail
tail.next=myNewNode
然后更新
tail
tail=myNewNode。我在周五回答说,只要在google上搜索一个
LinkedList
java实现,就有这么多例子。你可以在java中使用类,就像在c或c++lol中使用struct一样。
boolean first = true;

//if lists l1, l2 still have a value, append to l3
while (l1 != null || l2 != null)
{
    // create the next node
    if (!first) {
        // create the next node and attach it to the current node
        l3.next = new ListNode(0);
        // we now work with the next node
        l3 = l3.next;
    } else {
        first = false;
    }

    //if l1.val + l2.val >= 10 from last iteration, carry over 1
    if (carryover)
    {
        l3.val += 1;
        carryover = false;
    }

    if (l1 != null)
    {
        l3.val += l1.val;
        l1 = l1.next;
    }

    if (l2 != null)
    {
        l3.val += l2.val;
        l2 = l2.next;
    }

    if (l3.val > 9)
    {
        l3.val -= 10;
        carryover = true;
    }
    System.out.println(l3.val);

}