04. Deletion and Reverse in Circular Linked List

The problem can be found at the following link: Question Link

Problem Description

Given a Circular Linked List, the task is to delete a given node (key) from the circular linked list and then reverse the circular linked list. The key may or may not be present in the list.

Note:

  • You don't have to print anything, just return the head of the modified list in each function.

  • Nodes may consist of duplicate values.

Examples:

Input: Linked List: 2->5->7->8->10, key = 8 Output: 10->7->5->2 Explanation: After deleting 8 from the given circular linked list, it has elements as 2, 5, 7, 10. Now, reversing this list will result in 10, 7, 5, 2 & the resultant list is also circular.

Input: Linked List: 1->7->8->10, key = 8 Output: 10->7->1 Explanation: After deleting 8 from the given circular linked list, it has elements as 1, 7, 10. Now, reversing this list will result in 10, 7, 1 & the resultant list is also circular.

Input: Linked List: 3->6->4->10, key = 9 Output: 10->4->6->3 Explanation: Since there is no key present in the list, simply reverse the list & the resultant list is also circular.

My Approach

  1. Reverse the Circular Linked List:

    • Use three pointers: prev, current, and nextNode to reverse the links in the circular linked list until current returns to the head.

  2. Delete the Node:

    • Traverse the circular linked list using a pointer, comparing each node's data with the key.

    • If the key is found, adjust the pointers to remove the node. Handle special cases for the head and for when the list becomes empty.

  3. Return the Modified List:

    • Ensure that the returned list remains circular after deletion and reversal.

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), as we traverse the linked list once to delete the node and once to reverse it, where n is the number of nodes in the list.

  • Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space.

Code (C++)

Code (Java)

Code (Python)

Contribution and Support

For discussions, questions, or doubts related to this solution, please visit my LinkedIn: Any Questions. Thank you for your input; together, we strive to create a space where learning is a collaborative endeavor.

⭐ Star this repository if you find it helpful or intriguing! ⭐


📍Visitor Count

Last updated