30(June) Delete node in Doubly Linked List
30. Delete Node in Doubly Linked List
The problem can be found at the following link: Question Link
Problem Description
Given a doubly linked list and a position x, the task is to delete the node at the given position (position starts from 1) and return the head of the doubly linked list.
Examples:
Input:
LinkedList = 1 <--> 3 <--> 4, x = 3Output:
1 3Explanation: After deleting the node at position 3, the linked list will be 1 <--> 3.
My Approach
Edge Cases:
If the head is
null, returnnull.If
xis 1, update the head to the next node and adjust the pointers.
Traversal:
Traverse the list to reach the
xth node.If the
xth node is the head, update the head and adjust pointers.If the
xth node is not the head, adjust the pointers of the previous and next nodes to bypass thexth node.
Deletion:
Delete the
xth node.Return the updated head of the list.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), as we might need to traverse up to the
nth node.Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space.
Code
C++
Java
Python
Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated