反转链表后再相加
两个链表生成相加链表
http://www.nowcoder.com/questionTerminal/c56f6c70fb3f4849bc56e33ff2a50b6b
public class Solution { /** * * @param head1 ListNode类 * @param head2 ListNode类 * @return ListNode类 */ public ListNode addInList (ListNode head1, ListNode head2) { // write code here head1 = reverse(head1); head2 = reverse(head2); ListNode head = new ListNode(-1); ListNode cur = head; int carry = 0; while(head1 != null || head2 != null) { int val = carry; if (head1 != null) { val += head1.val; head1 = head1.next; } if (head2 != null) { val += head2.val; head2 = head2.next; } cur.next = new ListNode(val % 10); carry = val / 10; cur = cur.next; } if (carry > 0) { cur.next = new ListNode(carry); } return reverse(head.next); } private ListNode reverse(ListNode head) { ListNode cur = head; ListNode pre = null; while(cur != null) { ListNode temp = cur.next; cur.next = pre; pre = cur; cur = temp; } return pre; } }