ADD Numbersmy SubmissionsQuestionTotal accepted:105768 Total submissions:494750 difficulty:medium
You are given, linked lists representing, and non-negative numbers. The digits is stored in reverse order and all of their nodes contain a single digit. ADD the numbers and return it as a linked list.
Input: (2, 4, 3) + (5, 6, 4)
Output:7, 0, 8
Test instructions: Two numbers are added with a linked list. Java is not familiar. I'm going to refuel, cxq!.
Public classSolution { Public StaticListNode addtwonumbers (listnode L1, ListNode L2) {ListNode L3=NewListNode (-1); ListNode L3head=L3; if(l1==NULL) returnL2; if(l2==NULL) returnL1; intCarry=0; while(l1!=NULL|| l2!=NULL){ if(l1!=NULL) {Carry+=L1.val; L1=L1.next; } if(l2!=NULL) {Carry+=L2.val; L2=L2.next; } L3.next=NewListNode (carry%10); Carry=carry/10; L3=L3.next; } if(carry==1) {L3.next=NewListNode (1); } returnL3head.next; } Public Static voidMain (string[] args) {listnode L1=NewListNode (2); L1.next=NewListNode (4); L1.next.next=NewListNode (3); ListNode L2=NewListNode (5); L2.next=NewListNode (6); L2.next.next=NewListNode (4); ListNode Anwer=addtwonumbers (L1,L2); System.out.println (Anwer.val+ "" +anwer.next.val+ "" +anwer.next.next.val); }}
Leetcode 002 Add-Numbers