Posts

Showing posts from February, 2023

Eliminate duplicates from LL

  Eliminate duplicates from LL Send Feedback You have been given a singly linked list of integers where the elements are sorted in ascending order. Write a function that removes the consecutive duplicate values such that the given list only contains unique elements and returns the head to the updated list.  Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first and the only line of each test case or query contains the elements(in ascending order) of the singly linked list separated by a single space.  Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element.  Output format : For each test case/query, print the resulting singly linked list of integers in a row, separated by a single space. Output for every test case will be printed in a seperate line. Constraints : 1 <= t <= 10^2

AppendLastNToFirst

AppendLastNToFirst Send Feedback You have been given a singly linked list of integers along with an integer 'N'. Write a function to append the last 'N' nodes towards the front of the singly linked list and returns the new head to the list. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case or query contains the elements of the singly linked list separated by a single space. The second line contains the integer value 'N'. It denotes the number of nodes to be moved from last to the front of the singly linked list. Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element. Output format : For each test case/query, print the resulting singly linked list of integers in a row, separated by a single space. Output for every test case will b

Find a Node in Linked List

Find a Node in Linked List Send Feedback You have been given a singly linked list of integers. Write a function that returns the index/position of integer data denoted by 'N' (if it exists). Return -1 otherwise. Note : Assume that the Indexing for the singly linked list always starts from 0. Input format : The first line contains an Integer 'T' which denotes the number of test cases. The first line of each test case or query contains the elements of the singly linked list separated by a single space. The second line contains the integer value 'N'. It denotes the data to be searched in the given singly linked list. Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence -1 would never be a list element. Output format : For each test case, return the index/position of 'N' in the singly linked list. Return -1, otherwise. Output for every test case will be printed in a separate line. Not

Delete node (recursive)

  Delete node (recursive) Send Feedback Given a singly linked list of integers and position 'i', delete the node present at the 'i-th' position in the linked list recursively.  Note : Assume that the Indexing for the linked list always starts from 0. No need to print the list, it has already been taken care. Only return the new head to the list.  input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case or query contains the elements of the singly linked list separated by a single space. The second line of input contains a single integer depicting the value of 'i'. Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element Output format : For each test case/query, print the elements of the updated singly linked list. Output for every test ca

Insert node (recursive)

  Insert node (recursive) Send Feedback You have been given a linked list of integers. Your task is to write a function that inserts a node at a given position, 'pos'. Note: Assume that the Indexing for the linked list always starts from 0. If the given position 'pos' is greater than length of linked list, then you should return the same linked list without any change. And if position 'pos' is equal to length of input linked list, then insert the node at the last position. Input format: The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case or query contains the elements of the linked list separated by a single space. The second line of each test case contains two space separated integers, that denote the value of 'pos' and the data to be inserted respectively.  Remember/Consider : While specifying the list elements for input, -1 indicat