Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Add two numbers as a linked list

# My first Daily Interview Pro tryout

#You are given two linked-lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

#Example:
#Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
#Output: 7 -> 0 -> 8
#Explanation: 342 + 465 = 807.

# Definition for singly-linked list.
class ListNode(object):
  def __init__(self, x):
    self.val = x
    self.next = None

class Solution:
  def addTwoNumbers(self, l1, l2, c = 0):

    temp_l1 = [];
    temp_l2 = [];
    temp_l3 = [];

    # Add linked list l1 & l2 to lists temp_l1 & temp_l2
    while l1:
        temp_l1.append(l1.val);
        l1 = l1.next;

    while l2:
        temp_l2.append(l2.val);
        l2 = l2.next;

    # Convert temp_l1 & temp_l2 to strings then to integers x and y
    strings = [str(integer) for integer in temp_l1]
    a_string = "". join(strings)
    x = int(a_string)

    strings = [str(integer) for integer in temp_l2]
    a_string = "". join(strings)
    y = int(a_string)

    # Count x and y together then convert the combine_int to a string
    combine_int = x + y;
    combine_str = str(combine_int);

    # Loop the combine_str and add each character to temp_l3
    for char in combine_str:
        temp_l3.append(int(char));

    # Reverse temp_l3 to get the value of 708 because
    # 243 + 564 = 807
    # 342 + 465 = 807
    temp_l3.reverse();

    # Create linked list with temp_l3
    l3 = ListNode(temp_l3[0]);
    l3.next = ListNode(temp_l3[1]);
    l3.next.next = ListNode(temp_l3[2]);

    return l3

l1 = ListNode(2)
l1.next = ListNode(4)
l1.next.next = ListNode(3)

l2 = ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)

result = Solution().addTwoNumbers(l1, l2)
while result:
  print(result.val)
  result = result.next
# 7 0 8
Comment

add two numbers linked list

var addTwoNumbers = function(l1, l2) {
  const head = new ListNode();
  let cursor = head;
  let carry = 0;
  while (l1 || l2 || carry) {
    cursor.next = new ListNode();
    cursor = cursor.next;
    let val = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
    carry = val >= 10 ? 1 : 0;
    cursor.val = val % 10;
    l1 = l1 ? l1.next : null;
    l2 = l2 ? l2.next : null;
  }
  return head.next;
};
Comment

Add two numbers as a linked list

# My first Daily Interview Pro tryout

#You are given two linked-lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

#Example:
#Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
#Output: 7 -> 0 -> 8
#Explanation: 342 + 465 = 807.

# Definition for singly-linked list.
class ListNode(object):
  def __init__(self, x):
    self.val = x
    self.next = None

class Solution:
  def addTwoNumbers(self, l1, l2, c = 0):

    temp_l1 = [];
    temp_l2 = [];
    temp_l3 = [];

    # Add linked list l1 & l2 to lists temp_l1 & temp_l2
    while l1:
        temp_l1.append(l1.val);
        l1 = l1.next;

    while l2:
        temp_l2.append(l2.val);
        l2 = l2.next;

    # Convert temp_l1 & temp_l2 to strings then to integers x and y
    strings = [str(integer) for integer in temp_l1]
    a_string = "". join(strings)
    x = int(a_string)

    strings = [str(integer) for integer in temp_l2]
    a_string = "". join(strings)
    y = int(a_string)

    # Count x and y together then convert the combine_int to a string
    combine_int = x + y;
    combine_str = str(combine_int);

    # Loop the combine_str and add each character to temp_l3
    for char in combine_str:
        temp_l3.append(int(char));

    # Reverse temp_l3 to get the value of 708 because
    # 243 + 564 = 807
    # 342 + 465 = 807
    temp_l3.reverse();

    # Create linked list with temp_l3
    l3 = ListNode(temp_l3[0]);
    l3.next = ListNode(temp_l3[1]);
    l3.next.next = ListNode(temp_l3[2]);

    return l3

l1 = ListNode(2)
l1.next = ListNode(4)
l1.next.next = ListNode(3)

l2 = ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)

result = Solution().addTwoNumbers(l1, l2)
while result:
  print(result.val)
  result = result.next
# 7 0 8
Comment

add two numbers linked list

var addTwoNumbers = function(l1, l2) {
  const head = new ListNode();
  let cursor = head;
  let carry = 0;
  while (l1 || l2 || carry) {
    cursor.next = new ListNode();
    cursor = cursor.next;
    let val = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
    carry = val >= 10 ? 1 : 0;
    cursor.val = val % 10;
    l1 = l1 ? l1.next : null;
    l2 = l2 ? l2.next : null;
  }
  return head.next;
};
Comment

PREVIOUS NEXT
Code Example
Python :: cookies in django 
Python :: boolean in python 
Python :: session has key python 3 
Python :: type conversion python 
Python :: how to set global variable in python function 
Python :: Python 3 program to find factorial of given number 
Python :: negative indexing in python 
Python :: indentation in python 
Python :: merge two lists python 
Python :: Create list of unique values from dictionary 
Python :: adding two strings together in python 
Python :: ngnix config 
Python :: python type annotations list of possible values 
Python :: function in python 
Python :: turtle python 
Python :: python svg viewing 
Python :: kmp algorithm 
Python :: excel with python 
Python :: find common string in two strings python 
Python :: convert number to reversed array of digits python 
Python :: how to find missing item in a list 
Python :: pip config proxy 
Python :: pd.merge duplicate columns remove 
Python :: flask migrate multiple heads 
Python :: python minecraft server python gui 
Python :: add colorbar matplotlib 
Python :: fetch json array from mysql django 
Python :: change gles3 to gles2 
Python :: ndarray python 
Python :: Flatten List in Python Using NumPy concatenate 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =