Javascript Coding Interview Questions
The easy approach to ‘Linked List’ and ‘Adding two numbers’ questions
3 min readMay 15, 2021
Given two numbers represented by two lists, which will return the sum of the list. The sum list is a list representation of the addition of two input numbers.
Input: List1: 9->4->2// represents number 249
List2: 6->5->4 // represents number 456
Output:
Resultant list: 5->0->7 // represents number 705
Explanation: 249 + 456 = 705
Solution
Let’s learn how to create Node, Node will have its own data and pointer which will point to the next Node.
// singly-linked list.
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
now, Let’s create two linked list, example -
// creating first list, actual number is 249
head1 = new Node(9);
head1.next = new Node(4);
head1.next.next = new Node(2);// creating second list, actual number is 456
head2 = new Node(6);
head2.next = new Node(5);
head2.next.next = new Node(4);