Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question
have been given a singly linked list of integers. Write a function that returns the index/position of
integer data denoted by 'N' (if it exists). Return -1 otherwise. Note :Assume that the Indexing for the
singly linked list always starts from 0.Input format :The first line contains an Integer 'T' which
denotes the number of test cases. The first line of each test case or query contains the elements of
the singly linked list separated by a single space. The second line contains the integer value 'N'. It
denotes the data to be searched in the given singly linked list. Remember/Consider :While specifying
the list elements for input, -1 indicates the end of the singly linked list and hence -1 would never be a
list element. Output format :For each test case, return the index/position of 'N' in the singly linked
list. Return -1, otherwise. Output for every test case will be printed in a separate line. Note:You do
not need to print anything; it has already been taken care of. Just implement the given function.
Constraints :1 <= T <= 10^20 <= M <= 10^5 Where 'M' is the size of the singly linked list. Time Limit: 1
secSample Input 1:23 4 5 2 6 1 9-1510 20 30 40 50 60 70-16Sample Output 1 :2-1 Explanation for
Sample Output 1:In test case 1, 'N' = 5 appears at position 2 (0-based indexing) in the given linked
list. In test case 2, we can see that 'N' = 6 is not present in the given linked list.Sample Input 2:21
-12345 2 619-16Sample Output 2 :-14 Explanation for Sample Output 2:In test case 1, we can see
that 'N' = 2 is not present in the given linked list. In test case 2, 'N' = 6 appears at position 4 (0-based
indexing) in the given linked
list.Solutions:///////////////
Following is the class structure of the Node class: class LinkedListNode<T> { T data;
LinkedListNode<T>next; public LinkedListNode(T data) { this.data = data; } }
***********/ public class Solution { public static
int findNode(Linked ListNode<Integer> head, int n) { LinkedListNode<Integer> node = head; int
count=0; while (node!=null) { if (node.data==n) { return count; } else { node=node.next; count++; } }
expand button
Transcribed Image Text:have been given a singly linked list of integers. Write a function that returns the index/position of integer data denoted by 'N' (if it exists). Return -1 otherwise. Note :Assume that the Indexing for the singly linked list always starts from 0.Input format :The first line contains an Integer 'T' which denotes the number of test cases. The first line of each test case or query contains the elements of the singly linked list separated by a single space. The second line contains the integer value 'N'. It denotes the data to be searched in the given singly linked list. Remember/Consider :While specifying the list elements for input, -1 indicates the end of the singly linked list and hence -1 would never be a list element. Output format :For each test case, return the index/position of 'N' in the singly linked list. Return -1, otherwise. Output for every test case will be printed in a separate line. Note:You do not need to print anything; it has already been taken care of. Just implement the given function. Constraints :1 <= T <= 10^20 <= M <= 10^5 Where 'M' is the size of the singly linked list. Time Limit: 1 secSample Input 1:23 4 5 2 6 1 9-1510 20 30 40 50 60 70-16Sample Output 1 :2-1 Explanation for Sample Output 1:In test case 1, 'N' = 5 appears at position 2 (0-based indexing) in the given linked list. In test case 2, we can see that 'N' = 6 is not present in the given linked list.Sample Input 2:21 -12345 2 619-16Sample Output 2 :-14 Explanation for Sample Output 2:In test case 1, we can see that 'N' = 2 is not present in the given linked list. In test case 2, 'N' = 6 appears at position 4 (0-based indexing) in the given linked list.Solutions://///////////// Following is the class structure of the Node class: class LinkedListNode<T> { T data; LinkedListNode<T>next; public LinkedListNode(T data) { this.data = data; } } ***********/ public class Solution { public static int findNode(Linked ListNode<Integer> head, int n) { LinkedListNode<Integer> node = head; int count=0; while (node!=null) { if (node.data==n) { return count; } else { node=node.next; count++; } }
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY