Repository navigation
Expand file tree
/
Copy pathTest21.java
More file actions
84 lines (79 loc) · 2.56 KB
/
Copy pathTest21.java
File metadata and controls
84 lines (79 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.baidou;
/**
* 【程序49】
* 给出两个非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字。
* 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
* 您可以假设除了数字 0 之外,这两个数都不会以 0开头。
/
*
*/
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class Test21 {
public static void main(String[] args) {
ListNode l1 = new ListNode(2);
ListNode l12 = new ListNode(4);
ListNode l13 = new ListNode(3);
l1.next = l12;
l12.next = l13;
ListNode l2 = new ListNode(5);
ListNode l21 = new ListNode(6);
ListNode l22 = new ListNode(4);
l2.next = l21;
l21.next = l22;
ListNode listNode = addTwoNumbers(l1,l2);
StringBuilder stringBuilder = null;
while(listNode !=null){ //指向位置是否为空
if(stringBuilder == null){
stringBuilder = new StringBuilder();
stringBuilder.append(listNode.val);
}else{
stringBuilder.append(" -> "+ listNode.val);
}
listNode = listNode.next; // 指向下一个节点
}
System.out.println(stringBuilder.toString());
}
/**
* 链表输出和
* @param l1
* @param l2
* @return
*/
public static ListNode addTwoNumbers(ListNode l1, ListNode l2){
int carry = 0; //进位
ListNode newListNode = new ListNode(0);
ListNode tmpListNode ;
tmpListNode = newListNode;
while(true){
ListNode listNode = new ListNode(0);
int tmp = l1.val + l2.val + carry;
if(tmp < 10){
listNode.val = tmp;
carry = 0;
}else{
listNode.val = tmp%10;
carry = 1;
}
tmpListNode.next = listNode;
tmpListNode = listNode;
if(l1.next ==null && l2.next == null &&carry == 0){
break;
}
if(l1.next != null){
l1 = l1.next;
}else{
l1 = new ListNode(0);
}
if( l2.next != null){
l2 = l2.next;
}else{
l2 = new ListNode(0);
}
}
return newListNode.next;
}
}