两链表生成相加链表
两个链表生成相加链表
http://www.nowcoder.com/questionTerminal/c56f6c70fb3f4849bc56e33ff2a50b6b
先反转再相加再反转
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head1 ListNode类 * @param head2 ListNode类 * @return ListNode类 */ public ListNode addInList (ListNode head1, ListNode head2) { // write code here if(head1==null){ return head2; } if(head2==null){ return head1; } ListNode newHead1=reverseNode(head1); ListNode newHead2=reverseNode(head2); ListNode cur1=newHead1; ListNode cur2=newHead2; int sum=0; int shi=0; int ge=0; ListNode yummy=new ListNode(-1); ListNode pre=yummy; while(cur1!=null&&cur2!=null){ sum+=cur1.val+cur2.val; shi=sum/10; ge=sum%10; ListNode cur=new ListNode(ge); pre.next=cur; pre=cur; sum=sum/10; cur1=cur1.next; cur2=cur2.next; } while(cur1!=null){ sum+=cur1.val; shi=sum/10; ge=sum%10; ListNode cur=new ListNode(ge); pre.next=cur; pre=cur; sum=sum/10; cur1=cur1.next; } while(cur2!=null){ sum+=cur2.val; shi=sum/10; ge=sum%10; ListNode cur=new ListNode(ge); pre.next=cur; pre=cur; sum=sum/10; cur2=cur2.next; } return reverseNode(yummy.next); } //反转链表 public ListNode reverseNode(ListNode head){ if(head==null||head.next==null){ return head; } ListNode dummy=new ListNode(-1); dummy.next=head; ListNode pre=dummy; while(head.next!=null){ ListNode next=head.next; head.next=next.next; next.next=pre.next; pre.next=next; } return dummy.next; } }