Please provide Comment and test cases. PLease see attachment Create a Linked List data structure by writing your own Linked List class which contains a node class. The node class will house the data (integer in this case) and a pointer to the next node element. Populate your linked list with the following integers and print it . 50, 11, 33, 21, 40, 71 No you do not need to print the commas ;) Delete N-th node from the end of the linked list and print the linked list after deletion. Here N = 2 Below is the expected output after deleting the second last element. 50, 11, 33, 21, 71 ATTN : Note : Here we do not know the length of the list. Complete the above deletion operation without calculating the length of the list. Your solution should only make a single pass through the linked list, adhering to O(n) time complexity overall and O(1) space complexity. Hint : Maintain two pointers : a ’Fast’ Pointer and a ’Slow’ pointer. Initialize both pointers to a dummy node which points to the head of the list. Then starting a counter from zero, move the ’fast’ pointer two places forward, to maintain a gap of two between the fast and slow pointers and then move both in tandem. Finally, when the fast pointer reaches the end of the list , the slow pointer will be at the third last node. You can now delete the second last node.
Java
Please provide Comment and test cases. PLease see attachment
Create a Linked List data structure by writing your own Linked List
class which contains a node class. The
node class will house the data (integer in this case) and a pointer to the next
node element. Populate your linked list with the following integers and print it .
50, 11, 33, 21, 40, 71
No you do not need to print the commas ;)
Delete N-th node from the end of the linked list and print the linked list after deletion. Here N = 2
Below is the expected output after deleting the second last element.
50, 11, 33, 21, 71
ATTN : Note : Here we do not know the length of the list.
Complete the above deletion operation without calculating the length of the list.
Your solution should only make a single pass through the linked list, adhering
to O(n) time complexity overall and O(1) space complexity.
Hint : Maintain two pointers : a ’Fast’ Pointer and a ’Slow’ pointer. Initialize both pointers to a dummy node which points to the head of the list.
Then starting a counter from zero, move the ’fast’ pointer two places forward,
to maintain a gap of two between the fast and slow pointers and then move
both in tandem. Finally, when the fast pointer reaches the end of the list , the
slow pointer will be at the third last node. You can now delete the second last
node.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images