Leetcode link: https://leetcode.com/problems/delete-node-in-a-linked-list
Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly.
It is guaranteed that the node to be deleted is not a tail node in the list.
Since we are only give the node that is to be deleted, we can only delete it by "updating" it. This involves copying the element's value into the current node and repeatedly do this till we reach the 2nd last node and we terminate the linked list there.
- Time complexity:
O(n)where n is the length of the linked list
- Space complexity:
O(1)as no auxillary space is used