2017-02-07 54 views
2

的保存起点,我希望保存节点基于链表即起点。链接列表使用节点而不是Java类实现,因为我向列表添加了更多元素,并且必须继续前往下一个节点。节点基于链表

public class Solution { 
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 
     int c = 0; 
     ListNode n = new ListNode(0); 
     ListNode l3 = n; //Node initialised to first node. 
     while (l1 != null || l2 != null || c != 0) 
     { 
      int sum = l1.val + l2.val + c; 
      c = sum/10; 
      sum = sum%10; 

      n = new ListNode(sum); 
      n = n.next; 
      l1 = l1.next; 
      l2 = l2.next; 
     } 

     return l3; 
    } 
} 

在上面的例子中,我使用l3这样做。但是,当我返回l3它被设置为列表中的最后一个节点。 我怎样才能防止它在列表中移动与n

-------- ----------编辑

以下是本文给出了更容易参考的问题:

现在给你两个非 - 空链表,表示两个非负数的整数。数字以相反的顺序存储,并且它们的每个节点都包含一个数字。添加这两个数字并将其作为 链接列表返回。

你可以假设这两个数字不包含任何前导零,除了 数0本身。

输入:(2 - > 4 - > 3)+(5 - > 6 - > 4)输出:7 - > 0 - > 8

+2

你想用这段代码做什么? – mc20

+0

这是Leetcode的练习题。 –

+0

请发布本文给出了问题的链接这里 – mc20

回答

1

看那些线

 n = new ListNode(sum); 
     n = n.next; 

这里,n是你当前节点的地址,设置为完全不同的地址。旧地址处的节点现在与新创建的节点完全无关。换句话说,每次迭代循环时,都会丢弃旧列表并开始另一个列表。当然,由于旧的节点不再被任何东西引用,垃圾收集器会杀死它们。只有第一个创建的节点仍然存在,因为l3仍然指向它。

你需要操纵当前节点(而不是替换它),然后链接一个新的节点,就像这样:

 n.val = sum 
     if(l1.next != null || l2.next != null){ 
      n.next = new ListNode(0); 
      n = n.next; 
     } 

顺便说一句,给您的变量的一些专有名词。像l1 - > list1,n - > currentNode,c - >带。短不好。可读性很好。

此外,全功能会崩溃时,列表不会具有相同的长度,因为你始终可以访问的值都当其中一个可能是空的。你应该去像

 int sum = c; 
     if(l1 != null){sum+=l1.val;} 
     if(l2 != null){sum+=l2.val;} 

在循环的开始和

 if(l1 != null){l1=l1.next;} 
     if(l2 != null){l2=l2.next;} 

末。

1

我有时间回去打扫一下了一点。希望能帮助到你。

class Solution { 
    /** 
    * add two numbers in linked list form, lowest digit first form 
    * ie 1->0->3->4 represents the integer number 4301 
    * given 7->8->9->1 and 5->6->7->9 this method will return 2->5->7->1->1 (1987+9765=11752) 
    * 
    * @param lhs the left hand integer 
    * @param rhs the right hand integer 
    * @return the sum of the two integers 
    */ 
    public ListNode<Integer> addTwoNumbers(final ListNode<Integer> lhs, 
             final ListNode<Integer> rhs) { 
    return addTwoNumbers(lhs, rhs, 0); 
    } 

    private static ListNode<Integer> addTwoNumbers(final ListNode<Integer> lhs, 
               final ListNode<Integer> rhs, 
               final int carry) { 
    int sum = carry; 
    sum += lhs == null ? 0 : lhs.getValue(); 
    sum += rhs == null ? 0 : rhs.getValue(); 
    if (sum == 0 && rhs == null && lhs == null) { 
     return null; 
    } else { 
     return new ListNode<>(
     addTwoNumbers(
      lhs == null ? null : lhs.getNext(), 
      rhs == null ? null : rhs.getNext(), 
      sum/10), 
     sum % 10); 
    } 
    } 

    public static void main(final String... args) { 
    final ListNode<Integer> lhs = ListNode.fromVarArgs(1, 9, 8, 7); 
    final ListNode<Integer> rhs = ListNode.fromVarArgs(9, 7, 6, 5); 
    System.out.print(lhs); 
    System.out.println("+"); 
    System.out.println(rhs); 
    System.out.println("="); 
    System.out.println(new Solution().addTwoNumbers(lhs, rhs)); 
    } 
} 

class ListNode<T> { 
    static <T> ListNode<T> fromVarArgs(T... digits) { 
    ListNode<T> ret = null; 
    for (final T digit : digits) { 
     ret = new ListNode<>(ret, digit); 
    } 
    return ret; 
    } 

    private ListNode<T> next; 
    final private T value; 

    ListNode(final ListNode<T> next, final T value) { 
    this.next = next; 
    this.value = value; 
    } 

    ListNode(final T value) { 
    this(null, value); 
    } 

    ListNode<T> getNext() { 
    return next; 
    } 

    void setNext(final ListNode<T> next) { 
    this.next = next; 
    } 

    T getValue() { 
    return value; 
    } 

    @Override 
    public String toString() { 
    if (getNext() == null) { 
     return getValue().toString(); 
    } else { 
     return String.format(
     "%s -> %s", 
     getValue().toString(), 
     getNext().toString()); 
    } 
    } 
}