將兩個(gè)數(shù)字相加是一項(xiàng)簡(jiǎn)單的任務(wù),但如果數(shù)字以鏈表的形式給出,則可能會(huì)很棘手。鏈表的每個(gè)節(jié)點(diǎn)從第一個(gè)節(jié)點(diǎn)到最后一個(gè)節(jié)點(diǎn)以連續(xù)的方式包含它所代表的數(shù)字的數(shù)字。我們將得到兩個(gè)代表兩個(gè)不同數(shù)字的鏈表,我們必須將它們相加并以鏈表的形式返回第三個(gè)數(shù)字。
輸入
1 -> 2 -> 3 -> null 3 -> 2 -> 4 -> null
登錄后復(fù)制
輸出
4 -> 4 -> 7 -> null
登錄后復(fù)制
說(shuō)明:給定第一個(gè)數(shù)是123,第二個(gè)數(shù)是324,它們的和是447,我們以鏈表的形式返回。
轉(zhuǎn)換為數(shù)字方法
在這種方法中,首先,我們將給定的數(shù)字從鏈表表示形式轉(zhuǎn)換為整數(shù)形式,然后應(yīng)用加法運(yùn)算。之后,我們將結(jié)果轉(zhuǎn)換為鏈表,最后返回打印答案鏈表中存在的數(shù)據(jù)。
示例
// 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);
登錄后復(fù)制
輸出
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
登錄后復(fù)制登錄后復(fù)制
時(shí)間和空間復(fù)雜度
上述代碼的時(shí)間復(fù)雜度為(M+N),其中M和N是給定鏈表的大小。
上面代碼的空間復(fù)雜度是O(N),因?yàn)槲覀儎?chuàng)建了一個(gè)新的鏈表。
另一種方法
在這種方法中,我們將通過(guò)從末尾遍歷到第一個(gè)節(jié)點(diǎn)來(lái)添加鏈表元素,直到第一個(gè)鏈表值變?yōu)榱恪.?dāng)once變?yōu)榱銜r(shí),將其值為零并移動(dòng),直到它們都變?yōu)榱恪?/p>
示例
// 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);
登錄后復(fù)制
輸出
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
登錄后復(fù)制登錄后復(fù)制
結(jié)論
在本教程中,我們實(shí)現(xiàn)了 JavaScript 代碼來(lái)將兩個(gè)以鏈表形式給出的數(shù)字相加,并以鏈表形式返回結(jié)果。我們實(shí)現(xiàn)了兩種方法,時(shí)間和空間復(fù)雜度均為 O(N)。
以上就是JavaScript 程序添加由鏈接列表表示的兩個(gè)數(shù)字 – 設(shè)置 1的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!






