find the triplet sum in java linked list
Java
// Java program to find a triplet from three linked lists with
// sum equal to a given number
class LinkedList
{
Node head; // head of list
/* Linked list Node*/
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
/* A function to chech if there are three elements in a, b
and c whose sum is equal to givenNumber. The function
assumes that the list b is sorted in ascending order and
c is sorted in descending order. */
boolean isSumSorted(LinkedList la, LinkedList lb, LinkedList lc,
int givenNumber)
{
Node a = la.head;
// Traverse all nodes of la
while (a != null)
{
Node b = lb.head;
Node c = lc.head;
// for every node in la pick 2 nodes from lb and lc
while (b != null && c!=null)
{
int sum = a.data + b.data + c.data;
if (sum == givenNumber)
{
System.out.println("Triplet found " + a.data +
" " + b.data + " " + c.data);
return true;
}
// If sum is smaller then look for greater value of b
else if (sum < givenNumber)
b = b.next;
else
c = c.next;
}
a = a.next;
}
System.out.println("No Triplet found");
return false;
}
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
/* Driver program to test above functions */
public static void main(String args[])
{
LinkedList llist1 = new LinkedList();
LinkedList llist2 = new LinkedList();
LinkedList llist3 = new LinkedList();
/* Create Linked List llist1 100->15->5->20 */
llist1.push(20);
llist1.push(5);
llist1.push(15);
llist1.push(100);
/*create a sorted linked list 'b' 2->4->9->10 */
llist2.push(10);
llist2.push(9);
llist2.push(4);
llist2.push(2);
/*create another sorted linked list 'c' 8->4->2->1 */
llist3.push(1);
llist3.push(2);
llist3.push(4);
llist3.push(8);
int givenNumber = 25;
llist1.isSumSorted(llist1,llist2,llist3,givenNumber);
}
} /* This code is contributed by Rajat Mishra */
Also in Java:
- Title
- java get command line input
- Category
- Java
- Title
- java scanner next()
- Category
- Java
- Title
- html top padding
- Category
- Java
- Title
- how to create array of linkedlist in java
- Category
- Java
- Title
- initialize arraylist
- Category
- Java
- Title
- java foreach
- Category
- Java
- Title
- java methods
- Category
- Java
- Title
- how to check if in array java
- Category
- Java
- Title
- array in java stack overflow
- Category
- Java
- Title
- java string to boolean
- Category
- Java
- Title
- how to extract data from delimited text file in java
- Category
- Java
- Title
- web app runtime for java
- Category
- Java
- Title
- how to install java 8 on terminal os
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- java binary exponentiation
- Category
- Java
- Title
- how to create a 2d arraylist java
- Category
- Java
- Title
- changing double space to single in java script
- Category
- Java
- Title
- java switch case
- Category
- Java
- Title
- java create map
- Category
- Java
- Title
- how to cut a certion part from a string in java
- Category
- Java
- Title
- in java how to compare two strings
- Category
- Java
- Title
- basic java coding
- Category
- Java
- Title
- android hide and show bottom navigation
- Category
- Java
- Title
- java if statement string equals scanner
- Category
- Java
- Title
- how to take max value from priority queue in java
- Category
- Java
- Title
- context error in android studio
- Category
- Java
- Title
- how to make int array java android
- Category
- Java
- Title
- how to multiply a number by itself using for loop in java
- Category
- Java
- Title
- remove duplicates from singly linked list
- Category
- Java
- Title
- write an object java in a file
- Category
- Java
- Title
- print in one line in java
- Category
- Java
- Title
- How do you count characters in a string array in Java?
- Category
- Java
- Title
- java method
- Category
- Java
- Title
- how to add an item to a list in python
- Category
- Java
- Title
- print hello world in java
- Category
- Java
- Title
- firebase connecten
- Category
- Java
- Title
- ujava saum of positive integers
- Category
- Java
- Title
- how to loop through an array
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- java flood fill
- Category
- Java
- Title
- get cursor position in textarea java
- Category
- Java
- Title
- for loop java
- Category
- Java
- Title
- java setroundingmode
- Category
- Java
- Title
- java php object
- Category
- Java
- Title
- android create notification
- Category
- Java
- Title
- android java retrofit offline cache
- Category
- Java
- Title
- print out value java
- Category
- Java
- Title
- java remove duplicates
- Category
- Java
- Title
- java resource file
- Category
- Java
- Title
- fragment button nullpointerexception
- Category
- Java
- Title
- java sleep in code
- Category
- Java
- Title
- download spring
- Category
- Java
- Title
- java check if element exists in array
- Category
- Java
- Title
- android java how to stop users fromgoing back too much
- Category
- Java
- Title
- swapping of two numbers in java
- Category
- Java
- Title
- springBoot
- Category
- Java
- Title
- iterate over map keys java
- Category
- Java
- Title
- java nextpermutation
- Category
- Java
- Title
- how to change the color of a menubar in java
- Category
- Java
- Title
- most common element in list java
- Category
- Java
- Title
- org.mapstruct maven dependency
- Category
- Java
- Title
- how to collect objective in java
- Category
- Java
- Title
- java classes and methods
- Category
- Java
- Title
- hello world java
- Category
- Java
- Title
- stream java example
- Category
- Java
- Title
- how to check if a string is in alphabetical order in java
- Category
- Java
- Title
- java method to capitalize first letter
- Category
- Java
- Title
- how to create a circle in java
- Category
- Java
- Title
- bootstrap alert
- Category
- Java
- Title
- iterate hashmap java
- Category
- Java
- Title
- java create a set with values
- Category
- Java
- Title
- how to make a pre set list java
- Category
- Java
- Title
- java map example
- Category
- Java
- Title
- Java sort Map by values
- Category
- Java
- Title
- java random number in range
- Category
- Java
- Title
- create an empty array in java
- Category
- Java
- Title
- android notification addaction example
- Category
- Java
- Title
- Java array nested equals
- Category
- Java
- Title
- checking if a given integer is a prime number java
- Category
- Java
- Title
- pascal's triangle java 2d array
- Category
- Java
- Title
- action on long press of edit edittext
- Category
- Java
- Title
- Matlab java.lang.IllegalArgumentException: Invalid range
- Category
- Java
- Title
- java delay
- Category
- Java
- Title
- java get keys from hashmap
- Category
- Java
- Title
- how to delete character in string java
- Category
- Java
- Title
- read csv java
- Category
- Java
- Title
- java list all non directory files in the directory
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- in javascipt how to stop further page processing
- Category
- Java
- Title
- how to create an abstract class in java
- Category
- Java
- Title
- java pass array as method parameter
- Category
- Java
- Title
- java 8 group a collections by 2 property
- Category
- Java
- Title
- how to convert char to uppercase java
- Category
- Java
- Title
- java replace all html tags
- Category
- Java
- Title
- java cast duration to long
- Category
- Java
- Title
- rock paper scissors java
- Category
- Java
- Title
- link to method javadoc
- Category
- Java
- Title
- print in java
- Category
- Java
- Title
- what is static setter and getter examples in java
- Category
- Java
- Title
- import collections in java
- Category
- Java