Leetcode-add Numbers

Source: Internet
Author: User
Tags add numbers

ADD Numbers

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

Two numbers exist in two single-linked lists, asking for their and, as a result, there is a linked list.

Java programs

/*** 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 Headnode=NewListNode (0); ListNode CurrentNode=Headnode; intCArray = 0;  while(L1! =NULL&& L2! =NULL) {CArray+=L1.val; L1=L1.next; CArray+=L2.val; L2=L2.next; Currentnode.next=NewListNode (carray%10); CurrentNode=Currentnode.next; CArray/=10; }        if(l1!=NULL){                            while(l1!=NULL) {CArray+=L1.val; L1=L1.next; Currentnode.next=NewListNode (carray%10); CurrentNode=Currentnode.next; CArray/=10; }            }                    if(l2!=NULL){                 while(l2!=NULL) {CArray+=L2.val; L2=L2.next; Currentnode.next=NewListNode (carray%10); CurrentNode=Currentnode.next; CArray/=10; }            }        if(carray==1) {Currentnode.next=NewListNode (1); CurrentNode=Currentnode.next; }       returnHeadnode.next; }}

First two nodes, one pointing to the head node, one to the current running node

Simultaneously traverse two single linked list, corresponding position sum, carray%10 as new node value, CARRAY/10 is to carry

When a linked list is empty, the description is two different lengths of the number added, the current node to consider rounding the case, the non-empty nodes are linked together.

The upper while is the operation when the two linked list is not empty, and then joins the non-empty list

You can combine the two together.

/*** 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 Headnode=NewListNode (0); ListNode CurrentNode=Headnode; intCArray = 0;  while(L1! =NULL|| L2! =NULL){              if(l1!=NULL) {CArray+=L1.val; L1=L1.next; }            if(l2!=NULL) {CArray+=L2.val; L2=L2.next; } Currentnode.next=NewListNode (carray%10); CurrentNode=Currentnode.next; CArray/=10; }                if(carray==1) {Currentnode.next=NewListNode (1); CurrentNode=Currentnode.next; }       returnHeadnode.next; }}

This is a much simpler procedure.

Python program

#Definition for singly-linked list.#class ListNode (object):#def __init__ (self, x):#self.val = x#Self.next = Noneclasssolution (object):defaddtwonumbers (self, L1, L2):""": Type L1:ListNode:type l2:ListNode:rtype:ListNode"""Carry=0 Head=listnode (0) CurrentNode=Head whileL1orL2:ifL1:carry+=l1.val L1=L1.nextifL2:carry+=L2.val L2=L2.next Currentnode.next= ListNode (carry%10) CurrentNode=Currentnode.next Carry= CARRY//10ifCarry==1: Currentnode.next= ListNode (1) CurrentNode=Currentnode.nextreturnHead.next

Leetcode-add Numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.