Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Finding a Triplet from Three Linked Lists with a Sum Equal to a Given Number
In this article, we are going to implement a JavaScript program for finding a triplet from three linked lists with a sum equal to a given number. This problem is a variation of the standard three-sum problem but applied to linked lists instead of arrays.
Problem Statement
Given three linked lists and a target number, we need to find if there exists a triplet (one element from each list) whose sum equals the target number. If such a triplet exists, return true; otherwise, return false.
Example:
Target: 10 List 1: 1 ? 2 ? 3 ? 4 ? 5 List 2: 3 ? 7 ? 8 ? 13 List 3: 9 ? 7 ? 2 ? 8
Valid triplets with sum = 10:
- 1 (List 1) + 7 (List 2) + 2 (List 3) = 10
- 2 (List 1) + 3 (List 2) + 5 (List 3) = 10
Note: We must pick exactly one element from each of the three lists.
Approach
We'll use a two-pointer technique for optimization. The algorithm assumes:
- List 1: Can be unsorted
- List 2: Sorted in ascending order
- List 3: Sorted in descending order
If the lists aren't sorted as required, we can sort them using merge sort with O(N log N) time complexity.
Algorithm Steps
- Traverse each node in List 1
- For each node in List 1, use two pointers on List 2 and List 3
- Calculate current sum = node1 + node2 + node3
- If sum equals target, return true
- If sum
- If sum > target, move pointer in List 3 forward
Implementation
// Node class for linked list
class Node {
constructor(data) {
this.value = data;
this.next = null;
}
}
// Function to find triplet with given sum
function findTriplet(listA, listB, listC, target) {
let tempA = listA;
// Traverse all nodes of list A
while (tempA != null) {
let tempB = listB;
let tempC = listC;
// Use two pointer approach for listB and listC
while (tempB != null && tempC != null) {
let currentSum = tempA.value + tempB.value + tempC.value;
if (currentSum == target) {
console.log("Triplet found: " + tempA.value + " + " + tempB.value + " + " + tempC.value + " = " + target);
return true;
}
// If sum is smaller, move to next larger element in listB
else if (currentSum < target) {
tempB = tempB.next;
}
// If sum is larger, move to next smaller element in listC
else {
tempC = tempC.next;
}
}
tempA = tempA.next;
}
console.log("No triplet found with sum = " + target);
return false;
}
// Helper function to add node at beginning
function push(head, data) {
let newNode = new Node(data);
newNode.next = head;
return newNode;
}
// Create test linked lists
let headA = null;
headA = push(headA, 8);
headA = push(headA, 1);
headA = push(headA, 4);
headA = push(headA, 2);
// List B: sorted in ascending order (4, 10, 15, 20)
let headB = null;
headB = push(headB, 20);
headB = push(headB, 15);
headB = push(headB, 10);
headB = push(headB, 4);
// List C: sorted in descending order (10, 9, 4, 2)
let headC = null;
headC = push(headC, 2);
headC = push(headC, 4);
headC = push(headC, 9);
headC = push(headC, 10);
// Test with target sum
let target = 25;
findTriplet(headA, headB, headC, target);
// Test with a sum that exists
target = 16; // For example: 2 + 4 + 10 = 16
findTriplet(headA, headB, headC, target);
No triplet found with sum = 25 Triplet found: 2 + 4 + 10 = 16
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(N²) | N iterations for List A × N iterations for two-pointer approach |
| Space | O(1) | Only using pointers, no extra space |
Key Points
- The two-pointer technique works efficiently when List 2 is sorted ascending and List 3 is sorted descending
- If lists aren't sorted as required, sort them first using merge sort (additional O(N log N) time)
- This approach is more efficient than the brute force O(N³) solution
Conclusion
This JavaScript implementation efficiently finds triplets from three linked lists using the two-pointer technique. The O(N²) time complexity makes it suitable for moderate-sized linked lists, providing a significant improvement over brute force approaches.
