Reverse a Linked List in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:51:14

555 Views

Suppose we have a linked list, we have to reverse it. So if the list is like 2 -> 4 -> 6 -> 8, then the new reversed list will be 8 -> 6 -> 4 -> 2.To solve this, we will follow this approach −Define one procedure to perform list reversal in recursive way as solve(head, back)if head is not present, then return headtemp := head.nexthead.next := backback := headif temp is empty, then return headhead := tempreturn solve(head, back)Let us see the following implementation to get better understanding −Exampleclass ListNode:    def __init__(self, data, next = None):   ... Read More

Remove Consecutive Duplicate Characters in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:48:27

435 Views

Suppose we have a string s, we repeatedly delete the first consecutive duplicate characters. We have to find the final string.So, if the input is like s = "xyyyxxz", then the output will be "z", as "yyy" are the first consecutive duplicate characters which will be deleted. So we have "xxxz". Then "xxx" will be deleted to end up with "z".To solve this, we will follow these steps −stack := a new stacki := 0while i < size of s, doif stack is not empty and top of stack is same as s[i], thenx := delete last element from stackwhile ... Read More

Count Minimum Invalid Parenthesis to Make String Correct in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:45:21

451 Views

Suppose we have a string of parentheses; we have to write a function to compute the minimum number of parentheses to be removed to make the string correct (each open parenthesis is eventually closed).So, if the input is like "(()))(", then the output will be 2, as the correct string is "(())", remove ")(".To solve this, we will follow these steps −total := 0, temp := 0for each p in s, doif p is same as "(", thentotal := total + 1otherwise when p is same as ")" and total is not 0, thentotal := total - 1otherwise, temp := ... Read More

Remove Sublist to Get Same Number of Elements Below and Above K in C++

Arnab Chakraborty
Updated on 20-Oct-2020 10:43:04

176 Views

Suppose we have a list of numbers called nums and another number k, we can remove any sublist at most once from the list. We have to find the length of the longest resulting list such that the amount of numbers strictly less than k and strictly larger than k is the same.So, if the input is like nums = [6, 10, 8, 9, 3, 5], k = 6, then the output will be 5, as if we remove the sublist [9] then we will get [6, 10, 8, 3, 5] and there are two numbers [3, 5] which are ... Read More

Find Minimum Number of Intervals to Remove Overlaps in C++

Arnab Chakraborty
Updated on 20-Oct-2020 10:37:18

500 Views

Suppose we have a set of intervals; we have to find the minimum number of intervals that should be removed to make the rest of the intervals non-overlapping. So if the intervals are [[8, 10], [3, 5], [6, 9]], then the output will be 1, as we have to remove [6, 9] to make all others are non-overlapping.To solve this, we will follow these steps −n := size of arrayif n is 0, then return 0count := 1sort the array based on the end time of the intervalsend := end date of the first intervalfor i in range 1 to ... Read More

Remove Duplicate Entries in a Linked List in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:34:42

219 Views

Suppose we have a linked list of numbers, we have to remove those numbers which appear multiple times in the linked list (hold only one occurrence in the output), we also have to maintain the order of the appearance in the original linked list.So, if the input is like [2 -> 4 -> 6 -> 1 -> 4 -> 6 -> 9], then the output will be [2 -> 4 -> 6 -> 1 -> 9].To solve this, we will follow these steps −if node is not null, thenl := a new settemp := nodeinsert value of temp into lwhile ... Read More

Update Elements in a Given Range in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:31:20

349 Views

Suppose we have a list of numbers called nums and a list of operations. Here each operation has three fields [L, R, X], this indicated that we should increment by X all the elements from indices L to R (inclusive). We have to apply all operations and return the final list.So, if the input is like nums = [8, 4, 2, -9, 4] operations = [ [0, 0, 3], [1, 3, 2], [2, 3, 5] ], then the output will be [11, 6, 9, -2, 4], as the initial list was [8, 4, 2, -9, 4].Performing first operation [0, 0, ... Read More

Calculate Total Rainwater Collected in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:45:17

148 Views

Suppose we have an array of n non-negative integers. These are representing a height where the width of each bar is 1, we have to compute how much water it is able to catch after raining. So the map will be like −Here we can see there are 8 blue boxes, so the output will be 8.To solve this, we will follow these steps −Define a stack st, water := 0 and i := 0while i < size of heightif is stack is empty or height[stack top] >= height[i], then push i into stack, increase i by 1otherwisex := stack ... Read More

Find Maximum Product of Contiguous Subarray in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:43:33

1K+ Views

Suppose we have an array called nums, we have to find the product of elements of a contiguous subarray within an array (containing at least one number) which has the largest product. So if the array is [1, 9, 2, 0, 2, 5], the output will be 18, as contiguous subarray [1, 9, 2] has max product.To solve this, we will follow these steps −max_list := list of size nums, and fill with 0min_list := list of size nums, and fill with 0min_list := list of size nums, and fill with 0for i in range 1 to length of numsmax_list[i] ... Read More

Evaluate Postfix Notation in C++

Arnab Chakraborty
Updated on 20-Oct-2020 07:41:51

14K+ Views

Suppose we have postfix expression and we have to evaluate the value. Postfix expression is also known as Reverse polish notation. Here we have to use the stack data structure to solve the postfix expressions.So if the expression is “21+3*”, then the answer will be 9.Let us see the steps −for each character ch in the postfix expression, doif ch is an operator $\odot$ , thena := pop first element from stack, b := pop second element from the stackres := b $\odot$ apush res into the stackelse if ch is an operand, thenadd ch into the stackreturn element of ... Read More

Advertisements