检查节点元素的总和是否等于给定的键值
给定一个整数k和一个链表,链表的每个节点都由一对整数变量first和second 组成,用于保存数据,以及一个指向链表中下一个节点的指针。任务是找出任何节点的数据变量之和是否等于k 。如果是,则打印Yes否则打印No 。
例子:
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
方法:遍历整个链表,直到一个节点的元素之和等于key值。当节点元素的总和等于键值时,打印Yes 。如果不存在元素总和等于键值的节点,则打印No 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Represents node of the linked list
struct Node {
int first;
int second;
Node* next;
};
// Insertion in linked list
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;
}
}
// Function that returns true
// if the sum of element in a node = k
bool checkK(Node* head, int k)
{
// Check every node of the linked list
while (head != NULL) {
// If sum of the data of the current node = k
if ((head->first + head->second) == k)
return true;
// Get to next node in the list
head = head->next;
}
// No matching node found
return false;
}
// Driver code
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
// Java implementation of the approach
class GfG {
// Represents node of the linked list
static class Node {
int first;
int second;
Node next;
}
static Node head = null;
// Insertion in linked list
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;
}
}
// Function that returns true
// if the sum of element in a node = k
static boolean checkK(Node head, int k)
{
// Check every node of the linked list
while (head != null) {
// If sum of the data of the current node = k
if ((head.first + head.second) == k)
return true;
// Get to next node in the list
head = head.next;
}
// No matching node found
return false;
}
// Driver code
public static void main(String[] args)
{
// Node* head = NULL;
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");
}
}
// This code is contributed by Prerna Saini
Python3
# Python3 implementation of the approach
# Represents node of the linked list
class Node:
def __init__(self):
self.first = 0
self.second = 0
self.next = None
# Insertion in linked list
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
# Function that returns true
# if the sum of element in a node = k
def checkK(head, k):
# Check every node of the linked list
while (head != None):
# If sum of the data of the current node = k
if ((head.first + head.second) == k):
return True
# Get to next node in the list
head = head.next
# No matching node found
return False
# Driver code
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")
# This code is contributed by rutvik_56
C#
using System;
// c# implementation of the approach
public class GfG {
// Represents node of the linked list
public class Node {
public int first;
public int second;
public Node next;
}
public static Node head = null;
// Insertion in linked list
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;
}
}
// Function that returns true
// if the sum of element in a node = k
public static bool checkK(Node head, int k)
{
// Check every node of the linked list
while (head != null) {
// If sum of the data of the current node = k
if ((head.first + head.second) == k) {
return true;
}
// Get to next node in the list
head = head.next;
}
// No matching node found
return false;
}
// Driver code
public static void Main(string[] args)
{
// Node* head = NULL;
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
输出:
Yes
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。