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
/*** Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int X) {val = x;}}*/ Public classSolution { PublicListNode addtwonumbers (listnode L1, ListNode L2) {ListNode pl1=L1; ListNode PL2=L2; ListNode Head=NewListNode (0); ListNode P=Head; intCarry = 0; intsum; while(pl1!=NULL&& pl2!=NULL) {sum= Pl1.val + Pl2.val +carry; Carry= sum>=10?1:0; P.next=NewListNode (sum%10); P=P.next; PL1=Pl1.next; PL2=Pl2.next; } while(pl1!=NULL) {sum= Pl1.val +carry; Carry= sum>=10?1:0; P.next=NewListNode (sum%10); P=P.next; PL1=Pl1.next; } while(pl2!=NULL) {sum= Pl2.val +carry; Carry= sum>=10?1:0; P.next=NewListNode (sum%10); P=P.next; PL2=Pl2.next; } if(carry==1) {P.next=NewListNode (carry); } returnHead.next; }}
2. Add the Numbers