大家好,又见面了,我是你们的朋友全栈君。public class test {
public static void main(String[] args) { System.out.println(“Hello World!”); ListNode a = new ListNode(0); ListNode b = new ListNode(0); a.val = 2; a.next=new ListNode(4); a.next.next=new ListNode(5); b.val=5; b.next=new ListNode(6); b.next.next=new ListNode(4); ListNode c = addTwoNumbers(a,b);
} public static ListNode addTwoNumbers(ListNode a, ListNode b) { ListNode cListNode = new ListNode(0) ; ListNode c = cListNode,al=a,bl=b;//c存的是地址 c是指向cListNode的指针 int ext = 0; while(al!=null||bl!=null){ int va = (al!=null)?al.val:0; int vb = (bl!=null)?bl.val:0; int flag= va+vb+ext; ext =flag/10; c.next = new ListNode(flag%10); c = c.next; if(al!=null) al=al.next; if(bl!=null) bl=bl.next; } if(ext>0){ c.next = new ListNode(ext); } return cListNode.next; } }
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127706.html原文链接:https://javaforall.cn