2. Add Two Numbers

来源:互联网 发布:如何清理mac桌面图标 编辑:程序博客网 时间:2024/05/15 07:08

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8


public class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode head = new ListNode(0);ListNode p1 = l1, p2 = l2, p = head;int c = 0;while(p1!=null || p2!=null || c==1){int add1 = (p1==null ? 0 : p1.val);    //如果空则设值为0int add2 = (p2==null ? 0 : p2.val);int k = add1 + add2 + c;     //k用来存储和值c =  k/10;//进位p.next = new ListNode(k%10);p = p.next;if(p1!=null){p1 = p1.next;}//不空则下一个节点if(p2!=null){p2 = p2.next;}}return head.next;}}

0 0
原创粉丝点击