Check whether the sum of element of a node is equal to the given key value
Given an integer k and a linked list, every node of which consists of a pair of integer variable first and second to hold the data, and a pointer pointing to the next node in the list. The task is to find whether the sum of data variables of any of the nodes is equal to k. If yes then print Yes else print No.
Examples:
Input: (1, 2) -> (2, 3) -> (3, 4) -> (4, 5) -> NULL, k = 5
Output: Yes
For the second node, the sum of data variables is 2 + 3 = 5.
Input: (1, 2) -> (2, 3) -> (3, 4) -> (4, 5) -> NULL, k = 15
Output: No
Approach: Traverse the whole linked list until the sum of elements of a node is equal to the key value. When sum of element of a node is equal to key value then print Yes. If there is no such node whose sum of element is equal to the key value then print No.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int first;
int second;
Node* next;
};
void insert(Node** head, int f, int s)
{
Node* ptr = *head;
Node* temp = new Node();
temp->first = f;
temp->second = s;
temp->next = NULL;
if (*head == NULL)
*head = temp;
else {
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
bool checkK(Node* head, int k)
{
while (head != NULL) {
if ((head->first + head->second) == k)
return true ;
head = head->next;
}
return false ;
}
int main()
{
Node* head = NULL;
insert(&head, 1, 2);
insert(&head, 2, 3);
insert(&head, 3, 4);
insert(&head, 4, 5);
int k = 5;
if (checkK(head, k))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GfG {
static class Node {
int first;
int second;
Node next;
}
static Node head = null ;
static void insert( int f, int s)
{
Node ptr = head;
Node temp = new Node();
temp.first = f;
temp.second = s;
temp.next = null ;
if (head == null )
head = temp;
else {
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
}
static boolean checkK(Node head, int k)
{
while (head != null ) {
if ((head.first + head.second) == k)
return true ;
head = head.next;
}
return false ;
}
public static void main(String[] args)
{
insert( 1 , 2 );
insert( 2 , 3 );
insert( 3 , 4 );
insert( 4 , 5 );
int k = 5 ;
if (checkK(head, k) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
class Node:
def __init__( self ):
self .first = 0
self .second = 0
self . next = None
def insert(head, f, s):
ptr = head
temp = Node()
temp.first = f
temp.second = s
temp. next = None
if (head = = None ):
head = temp
else :
while (ptr. next ! = None ):
ptr = ptr. next
ptr. next = temp
return head
def checkK(head, k):
while (head ! = None ):
if ((head.first + head.second) = = k):
return True
head = head. next
return False
if __name__ = = '__main__' :
head = None
head = insert(head, 1 , 2 )
head = insert(head, 2 , 3 )
head = insert(head, 3 , 4 )
head = insert(head, 4 , 5 )
k = 5
if (checkK(head, k)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GfG {
public class Node {
public int first;
public int second;
public Node next;
}
public static Node head = null ;
public static void insert( int f, int s)
{
Node ptr = head;
Node temp = new Node();
temp.first = f;
temp.second = s;
temp.next = null ;
if (head == null ) {
head = temp;
}
else {
while (ptr.next != null ) {
ptr = ptr.next;
}
ptr.next = temp;
}
}
public static bool checkK(Node head, int k)
{
while (head != null ) {
if ((head.first + head.second) == k) {
return true ;
}
head = head.next;
}
return false ;
}
public static void Main( string [] args)
{
insert(1, 2);
insert(2, 3);
insert(3, 4);
insert(4, 5);
int k = 5;
if (checkK(head, k) == true ) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
class Node {
constructor() {
this .first = 0;
this .second = 0;
this .next = null ;
}
}
function insert( f, s)
{
var ptr = head;
var temp = new Node();
temp.first = f;
temp.second = s;
temp.next = null ;
if (head == null )
head = temp;
else {
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
}
function checkK( head, k)
{
while (head != null ) {
if ((head.first + head.second) == k)
return true ;
head = head.next;
}
return false ;
}
var head = null ;
insert(1, 2);
insert(2, 3);
insert(3, 4);
insert(4, 5);
let k = 5;
if (checkK(head, k) == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N), where N is the number of nodes in the linked list.
Auxiliary Space: O(1), as constant space is being used.
Last Updated :
20 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...