In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use leetcode to achieve the sum of linked lists in golang. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Given two integers represented by a linked list, each node contains a digit.
These digits are stored in reverse, that is, one bit at the beginning of the linked list.
Write a function to sum the two integers and return the results in a linked list.
Example:
Enter: (7-> 1-> 6) + (5-> 9-> 2), that is, 617 + 295
Output: 2-> 1-> 9, i.e. 912
Advanced: assuming that these digits are stored in the forward direction, please do it again.
Example:
Enter: (6-> 1-> 7) + (2-> 9-> 5), that is, 617 + 295
Output: 9-> 1-> 2, i.e. 912
Problem-solving ideas
1, this topic needs to pay attention to a detail, carry mark
2, there is a position in front of each position, so it is relatively simple
3. It should be noted that the length of the two linked lists is not the same.
4, special treatment is required for the case of 1: 9999
Code implementation
/ * Definition for singly-linked list. * type ListNode struct {* Val int * Next * ListNode * * / func addTwoNumbers (L1 * ListNode, L2 * ListNode) * ListNode {if l1==nil {return L2} if l2==nil {return L1} r:=new (ListNode) tr:=r t1:=l1 t2:=l2 flag:=0
Tr.Val= (t1.Val+t2.Val+flag) flag= (t1.Val+t2.Val+flag) / 10 for t1.NextEntropnil & & t2.NextFortunnil {tr.Next=new (ListNode) tr=tr.Next t1=t1.Next t2=t2.Next
Tr.Val= (t1.Val+t2.Val+flag) flag= (t1.Val+t2.Val+flag) / 10}
For t1.NextDiagnil {tr.Next=new (ListNode) tr=tr.Next t1=t1.Next tr.Val= (t1.Val+flag) flag= (t1.Val+flag) / 10}
For t2.NextDiagnil {tr.Next=new (ListNode) tr=tr.Next t2=t2.Next tr.Val= (t2.Val+flag) flag= (t2.Val+flag) / 10}
If benchmark 0 {tr.Next=new (ListNode) tr=tr.Next tr.Val=flag}
Return r} the above is how to use leetcode to achieve linked list summation in golang. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.