Add Two Numbers

Add two numbers LeetCode Problem

Add two numbers and return the sum assuming the following

  • The numbers are non-negative integers or empty.
  • The digits are stored in reverse order where each node contains a single digit

Example:

List 1 (l1) = (2,4,3)

List 2 (l2) = (5,6,4)

Expected results = 708 (342 + 465)

Add_Two_Numbers <- function(l1, l2){

 
x <- as.numeric(paste(rev(l1), collapse = "")) + as.numeric(paste(rev(l2), collapse = ""))
  
  

unlist(strsplit(as.character(x), ""))
}      

> Add_Two_Numbers(l1 = c(2,4,3),l2 = c(5,6,4))
[1] "8" "0" "7"

Leave a Reply

Your email address will not be published. Required fields are marked *