xxxxxxxxxx
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
};
xxxxxxxxxx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
string one = "398478949844984995987529438720390273984723247923092398072938792398023";
string two = "4375049085259283749374973290423984902834989837409739874928073948273979";
string Add = AddStrings(one,two);
Console.WriteLine("First Number : "+one);
Console.WriteLine("Second Number : " +two);
Console.WriteLine("After Addition : " +Add);
}
public static string AddStrings(string numberOne, string numberTwo)
{
var numberOneLength = numberOne.Length - 1;
var numberTwoLength = numberTwo.Length - 1;
if (numberOneLength > numberTwoLength)
{
return Add(numberOne, numberTwo, numberOneLength, numberTwoLength);
}
else
{
return Add(numberTwo, numberOne, numberTwoLength, numberOneLength);
}
}
public static string Add(string numberOne, string numberTwo, int numberOneLength, int numberTwoLength)
{
var stackOfAddedInteger = new Stack<int>();
var remainder = 0;
for (var i = 0; i <= numberOneLength; i++)
{
if (numberTwoLength - i >= 0)
{
remainder += Convert.ToInt32(numberTwo[numberTwoLength - i].ToString());
}
remainder += Convert.ToInt32(numberOne[numberOneLength - i].ToString());
stackOfAddedInteger.Push(remainder % 10);
remainder /= 10;
}
if (remainder != 0)
{
stackOfAddedInteger.Push(remainder);
}
var finalNumber = new StringBuilder();
while (stackOfAddedInteger.Count > 0)
{
finalNumber.Append(stackOfAddedInteger.Pop().ToString());
}
return finalNumber.ToString();
}
}
}
xxxxxxxxxx
package org.redquark.tutorials.leetcode;
/**
* @author Anirudh
*/
public class AddTwoNumbers {
private static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// Head of the new linked list - this is the head of the resultant list
ListNode head = null;
// Reference of head which is null at this point
ListNode temp = null;
// Carry
int carry = 0;
// Loop for the two lists
while (l1 != null || l2 != null) {
// At the start of each iteration, we should add carry from the last iteration
int sum = carry;
// Since the lengths of the lists may be unequal, we are checking if the
// current node is null for one of the lists
if (l1 != null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 != null) {
sum += l2.val;
l2 = l2.next;
}
// At this point, we will add the total sum % 10 to the new node
// in the resultant list
ListNode node = new ListNode(sum % 10);
// Carry to be added in the next iteration
carry = sum / 10;
// If this is the first node or head
if (temp == null) {
temp = head = node;
}
// For any other node
else {
temp.next = node;
temp = temp.next;
}
}
// After the last iteration, we will check if there is carry left
// If it's left then we will create a new node and add it
if (carry > 0) {
temp.next = new ListNode(carry);
}
return head;
}
}
xxxxxxxxxx
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
}
};
xxxxxxxxxx
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
}
}
xxxxxxxxxx
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
}
xxxxxxxxxx
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
}
}
xxxxxxxxxx
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
};
xxxxxxxxxx
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val = 0, _next = nil)
# @val = val
# @next = _next
# end
# end
# @param {ListNode} l1
# @param {ListNode} l2
# @return {ListNode}
def add_two_numbers(l1, l2)
end
xxxxxxxxxx
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
}
}