代码实现+测试用例
package LeetCode.addTwoNumber;
class Solution {
public static void main(String[] args) {
ListNode l1=new ListNode(2);
l1.next=new ListNode(4);
l1.next.next=new ListNode(3);
ListNode l2=new ListNode(5);
l2.next=new ListNode(6);
l2.next.next=new ListNode(4);
System.out.println(printListNode(new Solution().addTwoNumber(l1,l2)).toString());
}
//创建链表
public static ListNode createNode(int ...x){
ListNode l=new ListNode(0); //创建头节点 目的是记录链表索引为0的第一个节点的位置
ListNode node=l; //指向头节点
if (x.length>0){
for(int i:x){
node.next=new ListNode(i); //将创建的节点连接起来,记录到node 的地址域
node=node.next; //然后当前节点后移
//ListNode node=new ListNode(i)
//nextnode.next=node;
// nextNode=nextNode.next;
参考链接🔗https://www.cnblogs.com/easyidea/p/13371863.html
}
return l.next; //头节点记录了 连接后的2->4->3 5->6->4
}
else return null;
}
//打印链表
public static StringBuilder printListNode(ListNode listNode){
StringBuilder stringBuilder=new StringBuilder();
while (listNode !=null){
stringBuilder.append(listNode.val); //当前的节点
listNode=listNode.next; //当前节点的下一个节点
}
return stringBuilder;
}
static class ListNode {
ListNode next; //链表指向的下一个值的指针
int val;
ListNode(int x) { val = x; } //这个方式赋值
}
public ListNode addTwoNumber(ListNode l1,ListNode l2){
ListNode head = null, tail = null; // head<=>nodeSta tail相当于 nextnode移动过程中指向当前节点
int carry = 0; //表述进位(0或1)
while (l1 != null || l2 != null) {
int n1 = l1 != null ? l1.val : 0;
int n2 = l2 != null ? l2.val : 0;
int sum = n1 + n2 + carry;
if (head == null) {
head = tail = new ListNode(sum % 10); //相加后的新链表head (相当于新链表l3)
} else {
tail.next = new ListNode(sum % 10); //head后面的节点(相当于nextnode)
tail = tail.next;
}
//l1: 1 2 3
//l2: 2 3 4 5
carry = sum / 10; // 7/10=0
if (l1 != null) { //两个链表相应个位相加,向后递进一位相加
l1 = l1.next;
}
if (l2 != null) { //两个链表相应个位相加,向后递进一位相加
l2 = l2.next;
}
}
if (carry > 0) { // carry 0或者1 进位
tail.next = new ListNode(carry);
}
return head;
}
}
//"代码同上 有些问题 (对比错误)"
// ListNode head=null;
// ListNode tail=null;
// int carry=0;
// while (l1!=null || l2!=null){
// int n1=l1 !=null ?l1.val:0;
// int n2=l2!=null ?l2.val:0;
// int sum=n1+n2+carry;
//
// if(head==null){
// head=tail=new ListNode(sum%10);
//
// }
// else{
tail=tail.next=new ListNode(sum%10);
// tail.next=new ListNode(sum%10);
// tail=tail.next;
//
// }
// }
// (carry = sum / 10; // 7/10=0
// if (l1 != null) { //向后递进一位相加
// l1 = l1.next;
// }
// if (l2 != null) { //向后递进一位相加
// l2 = l2.next;
// })
// if(carry>0){
// tail.next=new ListNode(carry);
// }
// return head;
// }
// public static void listNodePrint(ListNode listNode) {
//
// if (listNode == null) {
// return;
// }
//
// if (listNode.next == null) {
// System.out.println(listNode.val);
// return;
// }
//
// System.out.println(listNode.val);
// listNodePrint(listNode.next);
// }
new solution() 匿名函数
参考链接 Java的匿名函数 - 蔡地像徐坤 - 博客园 (cnblogs.com)
import java.util.HashSet; import java.util.Set; class Test { public static void main(String[] args) { for (int i = 0; i < 5; i++) { method(new Car()); } } public static void method(Car car){ car.num=3; car.color="白色"; System.out.println(car.num+"---"+car.color); } static class Car{ int num; String color; } }运行结果: