Skip to content

Instantly share code, notes, and snippets.

@rozer007
Created October 26, 2021 11:40
Show Gist options
  • Save rozer007/91a5f939e0854b9da6c50c1e43c4b4c6 to your computer and use it in GitHub Desktop.
Save rozer007/91a5f939e0854b9da6c50c1e43c4b4c6 to your computer and use it in GitHub Desktop.
Kth Node From End Of Linked List
Q1) How many Approach are there ?
ans : Two
i) Ue the length of the linked List.
ii) By maintaining two pointer.
Q2) What is the time complexity in this problem?
ans: The time complexity of the above solution is O(n) as we have traversed the linked list once. We have traversed it in two parts.
First we traversed k elements and then we traversed the remaining (size minus k) elements.
Q3) What is the space complexity in this problem?
ans: 0(1) as we have not used any extra space for the solution.
Hint 1: Try to use two pointer or find the size of the array and find the kth node
Hint 2: keep two pointers and update one with k steps ahead.
Hint 3: The kth node is the node where pointer with delay a of k steps is pointing.
Q1) What is the time complexity of the above code ?
a) O(1)
b) O(n^3)
c) O(n)
d) O(k)
Ans : c) O(n)
Q2) The three data members present int the Linked List class are :
a) display, getFirst, getLast
b) head, size, tail
c) addAt, addLast, removeLast
d) size, display, head
Ans : b) head, size, tail
Q3) Fill in the blank:
Node f=head;
Node s=head;
for(int i=0;i<k;i++)
{
f=______;
}
while(f.next!=null)
{
f=______;
s=s.next;
}
return s.data;
a) f.next, f.next.next
b) f.next.next, f.next
c) f.next.next, f.next.next
d) f.next, f.next
Ans: d) f.next, f.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment