using System; using System.Collections.Generic; /* public class ListNode { public int val; public ListNode next; public ListNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head1 ListNode类 * @param head2 ListNode类 * @return ListNode类 */ public ListNode addInList (ListNode head1, ListNode head2) { ListNode l1 = ReverseList(head1); ListNode l2 = ReverseList(head2); ListNode l3 = new ListNode(0); ListNode p3 = l3; int curx = 0; while(l1 != null || l2 != null){ if(l1 != null){ curx += l1.val; l1 = l1.next; } if(l2 != null){ curx += l2.val; l2 = l2.next; } p3.next = new ListNode(curx % 10); curx = curx / 10; p3 = p3.next; } if(curx != 0) p3.next = new ListNode(curx % 10); return ReverseList(l3.next); } public ListNode ReverseList(ListNode head) { ListNode slow = null; ListNode fast = head; ListNode nex; while(fast != null){ nex = fast.next; fast.next = slow; slow = fast; fast = nex; } return slow; } }