Java 保存基于节点的链表的起点
我希望保存基于节点的链表的起点,即使用节点而不是Java类实现的链表,因为我向列表中添加了更多元素,并且必须继续转到下一个节点才能这样做Java 保存基于节点的链表的起点,java,algorithm,linked-list,Java,Algorithm,Linked List,我希望保存基于节点的链表的起点,即使用节点而不是Java类实现的链表,因为我向列表中添加了更多元素,并且必须继续转到下一个节点才能这样做 public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int c = 0; ListNode n = new ListNode(0); ListNode l3 = n; //Node initia
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
在列表中移动
------编辑----
以下是leetcode中的问题,以便于参考:
给您两个非空的链表,表示两个非负
整数。数字以相反的顺序存储,每个数字
节点包含一个数字。将这两个数字相加,并将其作为
链表
您可以假设这两个数字不包含任何前导零,除了
数字0本身
输入:(2->4->3)+(5->6->4)输出:7->0->8
看那些台词
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->carry。短不好。可读性好
此外,当列表的长度不相同时,整个函数将崩溃,因为当其中一个可能为null时,您总是访问这两个列表的值。你应该像这样去
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;}
最后。我有时间回去清理一下。希望能有帮助
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());
}
}
}
类解决方案{
/**
*在链表形式中添加两个数字,最低数字第一种形式
*ie 1->0->3->4表示整数4301
*给定7->8->9->1和5->6->7->9,此方法将返回2->5->7->1->1(1987+9765=11752)
*
*@param lhs左整数
*@param rhs右整数
*@返回两个整数的和
*/
公共ListNode AddTwoNumber(最终ListNode lhs,
最终列表节点(rhs){
返回addTwoNumber(左、右、0);
}
专用静态ListNode AddTwoNumber(最终ListNode lhs,
最终列表节点rhs,
最终整数进位){
整数和=进位;
sum+=lhs==null?0:lhs.getValue();
sum+=rhs==null?0:rhs.getValue();
if(sum==0&&rhs==null&&lhs==null){
返回null;
}否则{
返回新的ListNode(
添加两个数字(
lhs==null?null:lhs.getNext(),
rhs==null?null:rhs.getNext(),
总数(千分之十),,
总数(10%);
}
}
公共静态void main(最终字符串…参数){
final ListNode lhs=ListNode.fromVarArgs(1,9,8,7);
final ListNode rhs=ListNode.fromVarArgs(9,7,6,5);
系统输出打印(lhs);
System.out.println(“+”);
系统输出打印LN(rhs);
System.out.println(“=”);
System.out.println(新解决方案().addTwoNumber(lhs,rhs));
}
}
类ListNode{
来自varargs的静态ListNode(T…位){
ListNode ret=null;
for(最后的T位数:位数){
ret=新列表节点(ret,数字);
}
返回ret;
}
私有listnodenext;
最终私有T值;
ListNode(下一个最终ListNode,最终T值){
this.next=next;
这个值=值;
}
ListNode(最终T值){
该值(null,value);
}
ListNode getNext(){
下一步返回;
}
void setNext(最终列表节点next){
this.next=next;
}
T getValue(){
返回值;
}
@凌驾
公共字符串toString(){
if(getNext()==null){
返回getValue().toString();
}否则{
返回字符串格式(
“%s->%s”,
getValue().toString(),
getNext().toString());
}
}
}
你想用这段代码做什么?这是Leetcode的一个实践问题。请在此发布Leetcode问题的链接帮助任何人????