Skip to content

Commit 81a55b7

Browse files
authored
Update Find a Node in LL
1 parent 302b829 commit 81a55b7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Course 2 - Data Structures in JAVA/Lecture 7 - Linked Lists I/Find a Node in LL

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,39 @@ Sample Output 2 :
3838
Explanation for Sample Input 2 :
3939
For the given singly linked list, considering the indices starting from 0, progressing in a left to right manner with a jump of 1, then the N = 6 appears at position 4.
4040
*/
41+
/*
42+
43+
Following is the Node class already written for the Linked List
44+
45+
class LinkedListNode<T> {
46+
T data;
47+
LinkedListNode<T> next;
48+
49+
public LinkedListNode(T data) {
50+
this.data = data;
51+
}
52+
}
53+
54+
*/
55+
56+
public class Solution {
57+
58+
public static int findNode(LinkedListNode<Integer> head, int n) {
59+
//Your code goes here
60+
LinkedListNode<Integer> node = head;
61+
int count=0;
62+
while (node!=null)
63+
{
64+
if (node.data==n)
65+
{
66+
return count;
67+
}
68+
else
69+
{
70+
node=node.next;
71+
count++;
72+
}
73+
}
74+
return -1;
75+
}
76+
}

0 commit comments

Comments
 (0)