In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to add the two numbers of data structure and algorithm". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. explanation
Given two non-empty linked lists to represent two non-negative integers. Digits are stored in reverse order, and each of their nodes stores only a single number. Returns a new linked list by adding the two numbers.
You can assume that neither of these numbers will start with zero except for the number 0.
Example:
Enter: (2-> 4-> 3) + (5-> 6-> 4)
Output: 7-> 0-> 8
Reason: 342 + 465 = 807
II. Solution reference
1. Swift language
Class AddTwoNumbers {func addTwoNumbers (L1: ListNode?, _ 12: ListNode?)-> ListNode? {var carry = 0, L1 = L1 L2 = L2 let dummy = ListNode (0) var node = dummy while L1! = nil | | L2! = nil | | carry! = 0 {if L1! = nil {carry + = L1room.Val L1 = L1room.next} if L2! = nil {carry + = L2room.Val L2 = l2room.next} node.next = ListNode (carry% 10) node = node.next! Carry = carry / 10} return dummy.next}}
2. JavaScript language
/ * Definition for singly-linked list. * function ListNode (val) {* this.val = val; * this.next = null; *} * / / * @ param {ListNode} L1 * @ param {ListNode} L2 * @ return {ListNode} * / var addTwoNumbers = function (L1, L2) {var add = 0, ans, head; while (L1 | | L2) {var a = L1? L1.val: 0, b = L2? L2.val: 0; var sum = a + b + add; add = ~ (sum / 10); var node = new ListNode (sum% 10); if (! ans) ans = head = node; else {head.next = node; head = node;} if (L1) L1 = L1.next; if (L2) L2 = l2.next } if (add) {var node = new ListNode (add); head.next = node; head = node;} return ans;}
3. Python language
# Definition for singly-linked list.# class ListNode (object): # def _ init__ (self, x): # self.val = x # self.next = Noneclass Solution (object): # maybe standard version def _ addTwoNumbers (self, L1) L2): "": type L1: ListNode: type L2: ListNode: rtype: ListNode "" p = dummy = ListNode (- 1) carry = 0 while L1 and L2: p.next = ListNode (l1.val + l2.val + carry) carry = p.next.val / 10 p.next.val% = 10 p = p.next L1 = l1.next L2 = l2.next res = L1 or L2 while res: p.next = ListNode (res.val + carry) carry = p.next.val / 10 p.next.val% = 10 p = p.next res = res.next if carry : p.next = ListNode (1) return dummy.next # shorter version def addTwoNumbers (self L1, L2): P = dummy = ListNode (- 1) carry = 0 while L1 or L2 or carry: val = (L1 and l1.val or 0) + (L2 and l2.val or 0) + carry carry = val / 10 p.next = ListNode (val% 10) L1 = L1 and l1.next L2 = L2 and l2.next p = p.next return dummy.next
4. Java language
Public class ListNode {public int val; public ListNode next; public ListNode (int I) {this.val = I;} public int val () {return val;}} public ListNode addTwoNumbers (ListNode L1, ListNode L2) {ListNode dummyHead = new ListNode (0); ListNode p = L1, Q = L2, curr = dummyHead; int carry = 0 While (p! = null | | Q! = null) {int x = (p! = null)? P.val: 0; int y = (Q! = null)? Q.val: 0; int sum = carry + x + y; carry = sum / 10; curr.next = new ListNode (sum% 10); curr = curr.next; if (p! = null) p = p.next; if (Q! = null) Q = q.next;} if (carry > 0) {curr.next = new ListNode (carry);} return dummyHead.next;}
5. C++ language
Class Solution {public: ListNode * addTwoNumbers (ListNode * L1, ListNode * L2) {int x = 0, y * 0, carry=0, sum=0; ListNode * h=NULL, * * tasking null; while (L1 blank null | | L2 blank null) {x = getValueAndMoveNext (L1); y = getValueAndMoveNext (L2); sum= carry + x + y ListNode * node = new ListNode (sum); * t = node; t = (& node- > next); carry = sum/10;} if (carry > 0) {ListNode * node = new ListNode (carry); * t = node;} return h } private: int getValueAndMoveNext (ListNode* & l) {int x = 0; if (l! = NULL) {x = l-> val; l = l-> next;} return x;}}
6. C language
# include # include # include / * Definition for singly-linked list. * / struct ListNode {int val; struct ListNode* next;}; static struct ListNode* addTwoNumbers (struct ListNode* L1, struct ListNode* L2) {int carry_num = 0; int first = 1; struct ListNode* res = NULL; struct ListNode* p = NULL; struct ListNode* prev = p; while (L1! = NULL | | L2! = NULL | | carry_num) {int sum = 0; int last_carry = carry_num If (L1! = NULL) {sum + = L1-> val; L1 = L1-> next;} if (L2! = NULL) {sum + = 12-> val; L2 = 12-> next;} if (sum > = 10) {sum-= 10; carry_num = 1 } else {carry_num = 0;} p = malloc (sizeof (* p)); if (first) {res = p; first = 0;} p-> val = sum + last_carry; if (p-> val > = 10) {p-> val-= 10; carry_num = 1 } p-> next = NULL; if (prev! = NULL) {prev- > next = p;} prev = p;} return res;} static struct ListNode * node_build (const char * digits) {struct ListNode * res, * p, * prev; int first = 1; int len = strlen (digits); const char * c = digits + len-1; prev = NULL While (len-- > 0) {p = malloc (sizeof (* p)); if (first) {first = 0; res = p;} p-> val = * Cmuri-'0mm; p-> next = NULL; if (prev! = NULL) {prev- > next = p;} prev = p } return res;} static void show (struct ListNode * ln) {int sum = 0, factor = 1; while (ln! = NULL) {sum + = ln- > val * factor; factor * = 10; ln = ln- > next;} printf ("% d", sum);} int main (int argc, char * * argv) {if (argc < 3) {fprintf (stderr, "Usage:. / test N1 N2") Exit (- 1);} struct ListNode * L1 = node_build (argv [1]); struct ListNode * L2 = node_build (argv [2]); struct ListNode * res = addTwoNumbers (L1, L2); show (L1); show (L2); show (res); return 0;} "how to add two numbers of data structure and algorithm" is here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.