将两个数字相加是一项简单的任务,但如果数字以链表的形式给出,则可能会很棘手。链表的每个节点从第一个节点到最后一个节点以连续的方式包含它所代表的数字的数字。我们将得到两个代表两个不同数字的链表,我们必须将它们相加并以链表的形式返回第三个数字。
输入
1 -> 2 -> 3 -> null 3 -> 2 -> 4 -> null
输出
4 -> 4 -> 7 -> null
说明:给定第一个数是123,第二个数是324,它们的和是447,我们以链表的形式返回。
转换为数字方法
在这种方法中,首先,我们将给定的数字从链表表示形式转换为整数形式,然后应用加法运算。之后,我们将结果转换为链表,最后返回打印答案链表中存在的数据。
示例
// class to create the structure of the nodes
class Node{
constructor(data){
this.value = data;
this.next = null;
}
}
// function to print the linked list
function print(head){
var temp = head;
var ans = ""
while(temp.next != null){
ans += temp.value;
ans += " -> "
temp = temp.next
}
ans += temp.value
ans += " -> null"
console.log(ans)
}
// function to add data in linked list
function add(data, head, tail){
return tail.next = new Node(data);
}
// function to convert linked list to number
function LL_to_int(head){
var temp = "";
var cur = head;
while(cur != null){
temp += cur.value.toString();
cur = cur.next;
}
return parseInt(temp);
}
// function to convert number to linked list
function num_to_LL(num){
var str = num.toString();
var head = new Node(str[0]-'0');
var tail = head;
for(var i = 1; i<str.length; i++){
tail = add(str[i]-'0',head, tail);
}
// final number is
console.log("The final answer is: ")
print(head);
}
// defining first number
var num1 = new Node(1)
var tail = num1
tail = add(2,num1, tail)
tail = add(3,num1, tail)
console.log("The given first number is: ")
print(num1)
// defining second number
var num2 = new Node(3)
tail = num2
tail = add(2,num2, tail)
tail = add(4,num2, tail)
console.log("The given second number is: ")
print(num2)
// converting both the linked list into the actual values
int_num1 = LL_to_int(num1)
int_num2 = LL_to_int(num2)
var ans = int_num1 + int_num2;
// converting number to the linked list
num_to_LL(ans);
输出
The given first number is: 1 -> 2 -> 3 -> null The given second number is: 3 -> 2 -> 4 -> null The final answer is: 4 -> 4 -> 7 -> null
时间和空间复杂度
上述代码的时间复杂度为(M+N),其中M和N是给定链表的大小。
上面代码的空间复杂度是O(N),因为我们创建了一个新的链表。
另一种方法
在这种方法中,我们将通过从末尾遍历到第一个节点来添加链表元素,直到第一个链表值变为零。当once变为零时,将其值为零并移动,直到它们都变为零。
示例
// class to create the structure of the nodes
class Node{
constructor(data){
this.value = data;
this.next = null;
}
}
// function to print the linked list
function print(head){
var temp = head;
var ans = ""
while(temp.next != null){
ans += temp.value;
ans += " -> "
temp = temp.next
}
ans += temp.value
ans += " -> null"
console.log(ans)
}
// function to add data in linked list
function add(data, head, tail){
return tail.next = new Node(data);
}
// function to convert string to linked list
function num_to_LL(str){
var head = new Node(str[str.length-1]-'0');
var tail = head;
for(var i = str.length-2; i>=0; i--){
tail = add(str[i]-'0',head, tail);
}
// final number is
console.log("The final answer is: ")
print(head);
}
// function to add values of the linked lists
function addLL(ll1, ll2){
var str = "";
var carry = 0;
while((ll1 != null) || (ll2 != null)){
if(ll1 == null){
carry += ll2.value;
ll2 = ll2.next;
} else if(ll2 == null){
carry += ll1.value;
ll1 = ll1.next;
} else {
carry += ll1.value + ll2.value;
ll2 = ll2.next;
ll1 = ll1.next;
}
str += (carry%10).toString();
carry /= 10;
carry = Math.floor(carry);
}
if(carry != 0){
str += (carry%10).toString();
}
// calling function to print the answer
num_to_LL(str);
}
// defining first number in reverse manner
var num1 = new Node(3)
var tail = num1
tail = add(2,num1, tail)
tail = add(1,num1, tail)
console.log("The given first number in reverse manner is: ")
print(num1)
// defining second number
var num2 = new Node(4)
tail = num2
tail = add(2,num2, tail)
tail = add(3,num2, tail)
console.log("The given second number in reverse manner is: ")
print(num2)
// calling to the add function
addLL(num1,num2);
输出
The given first number is: 1 -> 2 -> 3 -> null The given second number is: 3 -> 2 -> 4 -> null The final answer is: 4 -> 4 -> 7 -> null
结论
在本教程中,我们实现了 JavaScript 代码来将两个以链表形式给出的数字相加,并以链表形式返回结果。我们实现了两种方法,时间和空间复杂度均为 O(N)。
以上就是JavaScript 程序添加由链接列表表示的两个数字 – 设置 1的详细内容,更多请关注双恒网络其它相关文章!
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » JavaScript 程序添加由链接列表表示的两个数字
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » JavaScript 程序添加由链接列表表示的两个数字
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?