Remove String from Linked List Problem

You are provided with a singly linked list where each node contains a single character, along with a string 'STR'. Your task is to remove all occurrences of the string 'STR' from the linked list.

Explanation:

Start checking for the string 'STR' occurrences from the end of the linked list, not from the beginning. Remove the occurrences and ensure that you check for any new formations of 'STR' after removing elements.

Input:

The first line contains an integer 'T' representing the number of test cases. Each test case consists of two lines: the first line contains the elements of the singly linked list as characters not separated by space, and the second line contains the string 'STR'.

Output:

For each test case, return the head of the linked list after removing the specified string occurrences.

Example:

Input:
T = 1
Linked List: a b a b a
STR: aba
Output:
a b

Constraints:

  • 1 <= T <= 10
  • 0 <= N <= 10^3
  • 0 <= K <= 10^2
  • Where 'N' is the size of the singly linked list and 'K' is the size of the string.
  • Time limit: 1 second

Note:

You don't need to print the output, as it is handled for you. Just implement the relevant function.
AnswerBot
4mo

Remove all occurrences of a specified string from a singly linked list by checking from the end of the list.

  • Traverse the linked list from the end to efficiently remove the specified string occurrences...read more

Help your peers!
Select
Add answer anonymously...

Top Software Engineer Interview Questions Asked at MAQ Software

Q. How does hashing help in identifying duplicate values?
Q. What is the difference between span and div tags?
Q. Given an array and a number, determine whether the number can be generated using...read more
Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits