Is to add the numbers in two linked lists.

Source: Internet
Author: User

Think of the run in the sunset, that is my lost youth ---- never imagined

As soon as I saw it, I thought it was very simple, because it was the thought of adding large numbers. Later I learned about the various situations and adjusted it for more than two hours, so I was confused, but the leecode test case was very good.

1. The chain table is merged, which is similar to the ordered chain table,

2. After the merge, the large numbers are used to add up and the decimal place is exceeded. The last node needs special processing. I have written such a long code and a lot of repeated code. So I thought carefully and reconstructed the code;

When the linked list is merged, it can be directly added. It is okay to traverse it once. There is a small trick in the code twice before and after. If the header node is easy to change, you can create your own header node, and then remove it,

It is often used to solve linked list problems,

 

Method 1: High AC Redundancy

  1 /**  2  * Definition for singly-linked list.  3  * public class ListNode {  4  *     int val;  5  *     ListNode next;  6  *     ListNode(int x) {  7  *         val = x;  8  *         next = null;  9  *     } 10  * } 11  */ 12 public class Solution { 13     public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 14         ListNode head=null; 15         if(l1==null&&l2==null) return head; 16         head=new ListNode(l1.val+l2.val); 17         ListNode tail=head; 18         ListNode h1=l1.next; 19         ListNode h2=l2.next; 20         while(h1!=null&&h2!=null) 21         { 22             ListNode n2=new ListNode(h1.val+h2.val); 23             h1=h1.next; 24             h2=h2.next; 25             //insert into list  26             tail.next=n2; 27             tail=tail.next; 28              29         } 30         // the leght is same 31         if(h1!=null) 32         { 33             while(h1!=null) 34             { 35                 tail.next=h1; 36                 tail=tail.next; 37                 h1=h1.next; 38             } 39              40              41         } 42         if(h2!=null) 43         { 44             while(h2!=null) 45             { 46                 tail.next=h2; 47                 tail=tail.next; 48                 h2=h2.next; 49             } 50              51              52         } 53         //ceate a new Linklist 54          55        ListNode lhead=new ListNode(-10); 56        tail=lhead; 57         h1=head; 58         int s=0; 59         while(h1.next!=null) //utli the last poit 60         { 61             if(h1.val+s>=10)    //is large than 10 62             { 63                 int t=h1.val+s; 64                 h1.val=t%10; 65                 s=t/10; 66                  67             } 68             else 69             { 70                 h1.val=h1.val+s; 71                 s=0;  // I forget it ,so BEiJU 72                  73             } 74             //insert into list 75             tail.next=h1; 76             tail=tail.next; 77              78             h1=h1.next; 79              80              81              82         } 83         if(h1.val+s>=10) 84         { 85             int t2=h1.val+s; 86             h1.val=t2%10; 87             s=t2/10; 88             tail.next=h1; 89             tail=tail.next; 90              91             ListNode l4=new ListNode(s); 92             tail.next=l4; 93             tail=tail.next; 94              95         } 96         else 97         { 98             h1.val=h1.val+s; 99             tail.next=h1;100             tail=tail.next;101             102             103         }104         105         return lhead.next;106         107     108     }109 }

2. It took another hour to adjust the code, which is much more concise. When you write a lot of repeated code, it indicates that the code you write is faulty.

 1 /** 2  * Definition for singly-linked list. 3  * public class ListNode { 4  *     int val; 5  *     ListNode next; 6  *     ListNode(int x) { 7  *         val = x; 8  *         next = null; 9  *     }10  * }11  */12 public class Solution {13     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {14         ListNode head=new ListNode(-1);15         if(l1==null&&l2==null) return head.next;16         ListNode tail=head;17         int s=0;// jin wei 18 19         ListNode h1=l1;20         ListNode h2=l2;21         while(h1!=null&&h2!=null)22         {23             int t=h1.val+h2.val+s;24             ListNode h3=h1.next;25                 h1.val=t%10;26                 s=t/10;27             h1.next=null;28             tail.next=h1;29             tail=tail.next;30             h1=h3;31             h2=h2.next;32             //insert into list 33         }34        35         // the leght is same36         ListNode h=(h1!=null)?h1:h2;37         38         if(h!=null)39         {40             while(h!=null)41             {42                 int t=h.val+s;43                 h.val=t%10;44                 s=t/10;45                 ListNode tem=h.next;46                 h.next=null;47                 48                 tail.next=h;49                 50                 tail=tail.next;51                 h=tem;52             }53             54             55         }56        57             if(s>0)58             {59                 ListNode n=new ListNode(s);60                 tail.next=n;61                 tail=tail.next;62             }63     64         return head.next;65         66     }67 }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.