LeetCode [2]. Add Two Numbers -- java implementation, addtwonumbersjava

Source: Internet
Author: User

LeetCode [2]. Add Two Numbers -- java implementation, addtwonumbersjava

Add Two Numbers as follows:

You are given two linked lists representing two non-negative numbers. 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.

Input: (2-> 4-> 3) + (5-> 6-> 4)
Output: 7-> 0-> 8

Simply put, for two one-way linked lists, elements are added in reverse order and stored in the same rule. Pay attention to carry! The following is my java program: 1. General Practice: extract and compute one by one, and consider the situation where one of them reaches the end of the chain.
/*** Definition for singly-linked list. * public class ListNode {* int val; * ListNode next; * ListNode (in x) {val = x;} */public class Solution {public ListNode addTwoNumbers (ListNode l1, listNode l2) {ListNode slist = new ListNode (0); ListNode clist = slist; ListNode nlist = new ListNode (0); // int sval = 0; int flag = 0; // carry // 1. if First Node of l1 or l2 is null if (l1 = null | l2 = null) {Return (l1 = null )? (L2 = null )? (Slist) :( l2) :( l1);} // 2.1 When both l1 and l2 are not at the end of the chain, loop while (true) {clist. val = (l1.val + l2.val + flag) % (10); flag = (l1.val + l2.val + flag)/10; // next node l1 = l1.next; l2 = l2.next; // 2.1.1 if any one of them is the end of a chain, if (l1 = null | l2 = null) {break;} else {clist. next = new ListNode (0); clist = clist. next ;}}; // while // 2.2 if both are at the end of the chain, if (l1 = null & l2 = null) {// 2.2.1 if the two are at the end of the chain and have an increment, the result must be carried if (flag = 1) {nlist = new ListNode (fla G); clist. next = nlist;} else {return slist;} else // 2.2 one arrives at the end of the chain, one does not {ListNode onelist = new ListNode (0); if (l1 = null) {onelist = l2;} else {onelist = l1;} while (onelist! = Null) {clist. next = new ListNode (0); clist = clist. next; clist. val = (onelist. val + flag) % 10; flag = (onelist. val + flag)/10; onelist = onelist. next;} // 2.2.1 when the other one also reaches the end of the chain, determine whether there is a forward if (flag = 1) {clist. next = new ListNode (flag) ;}return slist ;}}

Ii. clear thinking: First read the linked list as a numerical type, and then add the results to the specified linked list. This method is very clear and simple, but whether it will overflow one by one, and the increase in time and space complexity. 3. Here is a better and more concise solution for reference: Leetcode-Add Two Numbers (Java ). This method is used to determine whether two linked lists are at the end of the chain. There is no need to consider multiple situations like one. It seems that many problems can be solved in a unified manner. Before you do this, you need to think more about whether this approach is available.


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.