You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: head1: 2->4->3->NULL, head2: 5->6->4->NULL
Output: 7->0->8->NULL
Approach
Java
public class AddTwoNumbers {public static void main(String[] args) {ListNode head1 = new ListNode(2);head1.next = new ListNode(4);head1.next.next = new ListNode(3);ListNode head2 = new ListNode(5);head2.next = new ListNode(6);head2.next.next = new ListNode(4);ListNode newlist = addTwoNumbers(head1, head2);linkedListTraversal(newlist);}private static void linkedListTraversal(ListNode node) {while (node != null) {System.out.print(node.val + " ");node = node.next;}}public static ListNode reverseList(ListNode head) {if (head == null || head.next == null)return head;ListNode c = head;ListNode prev = null;while (c != null) {ListNode n = c.next;c.next = prev;prev = c;c = n;}return prev;}// function to add two linked liststatic ListNode addTwoNumbers(ListNode l1, ListNode l2) {int x = 0, y = 0, z, l = 0;ListNode temp1 = l1, temp2 = l2, start = null, temp = null;while (temp1 != null || temp2 != null) {if (temp1 != null) {x = temp1.val;temp1 = temp1.next;} else if (temp1 == null)x = 0;if (temp2 != null) {y = temp2.val;temp2 = temp2.next;} else if (temp2 == null)y = 0;z = x + y + l;ListNode newNode = new ListNode(z % 10);newNode.next = null;if (start == null) {start = newNode;temp = start;} else {temp.next = newNode;temp = temp.next;}l = z / 10;}if (l > 0) {ListNode newNode = new ListNode(l);newNode.next = null;temp.next = newNode;temp = temp.next;}return start;}}class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) {this.val = val;}ListNode(int val, ListNode next) {this.val = val;this.next = next;}}
C++
#include <bits/stdc++.h>using namespace std;//Struture of nodestruct Node{int data;Node *next;Node(int data){this->data=data;this->next=NULL;}};Node* addTwoNumbers(Node* l1, Node* l2){int x=0,y=0,z,l=0;Node *temp1=l1,*temp2=l2,*start=NULL,*temp;while(temp1!=NULL || temp2!=NULL){if(temp1!=NULL){x=temp1->data;temp1=temp1->next;}else if(temp1==NULL)x=0;if(temp2!=NULL){y=temp2->data;temp2=temp2->next;}else if(temp2==NULL)y=0;z=x+y+l;Node *newNode=new Node(z%10);newNode->next=NULL;if(start==NULL){start=newNode;temp=start;}else{temp->next=newNode;temp=temp->next;}l=z/10;}if(l>0){Node *newNode=new Node(l);newNode->next=NULL;temp->next=newNode;temp=temp->next;}return start;}//function to traverse a linked//listvoid linkedListTraversal(Node *head){Node *temp=head;//iterate till we reach end of the linked//listwhile(temp!=NULL){//print the current//node datacout<<temp->data<<" ";//move to next nodetemp=temp->next;}}int main(){Node *head1=new Node(2);head1->next=new Node(4);head1->next->next=new Node(3);Node *head2=new Node(5);head2->next=new Node(6);head2->next->next=new Node(4);Node *add=addTwoNumbers(head1,head2);linkedListTraversal(add);return 0;}
No comments:
Post a Comment