Deletion in Circular Linked List

You are given a Circular Linked List of integers, and an integer, 'key'.

You have to write a function that finds the given key in the list and deletes it. If no such key is present, then the list remains unchanged.

For Example :
This is a visualization of the Circular Linked List, represented by:
1 2 3 4 5 -1

linked_list_1

Note :
The Circular Linked List before/after deletion may happen to be empty. In that case, only print -1.

All integers in the list are unique.


Input format :
The first input line contains the integers present in the circular linked list in order. 

The second input line contains a single integer 'key', the key to be deleted.
Remember/Consider :
-1 marks the end of the linked list however, the tail of the linked list would be pointing to the head making it circular in nature.
Output format :
The only output line contains the updated circular linked list post deletion.
Constraints :
0 <= N <= 10 ^ 5
1 <= key <= 10 ^ 5

Where 'N' is the length of the Circular Linked List.

Time limit: 1 sec
CodingNinjas
author
2y
Recursion

The idea is to use a recursive approach to find the given key and then remove that node. The recursive idea is very clear that we will traverse through the circular linked list, and we will c...read more

CodingNinjas
author
2y
Curr And Prev Approach

Here is complete algorithm:

  1. If the list is empty simply return.
  2. If the list is not empty, then we define two pointers curr and prev and initialize the pointer curr with the head n...read more
Help your peers!
Add answer anonymously...
Wolters Kluwer Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter