Given the head of a linked list, reverse the list and return the new head.
Examples:
Input:
Output: 5 -> 4 -> 3 -> 2 -> 1
CODE:
Here to reverse the linked list all we have to do is change the interconnection, that is 1 -> 2 to 2 -> 1 in every step and then once we reach the last node which is 5 we will change the new head as 5.
To achieve this, we will use three pointers. Each to change the direction of connection, current, previous and next. Current will be at center, previous behind the current and next after the current. Now current -> next will be previous, then what will be the use of next pointer.
Next pointer is used to traverse the linked list in original order. After changing the connection if current tries to move to it's next it we go back to 1 but we don't need that thus we make use of next.
MY CODE: In brute force method the intuitive way of solving this problem with two for loop. Using nested loop to traverse through the list and try different combination of two sum to check if it matches with the target. Time complexity: O(n^2) OPTIMIZED: In the optimized method of approaching the Two Sum problem. The given list is already sorted in ascending order. This gives us the leverage of using Two pointer technique to solve this problem. Thus using single loop to approach this problem. If current sum is less than target move the left pointer, if it is greater move the right pointer else return the current index (equal to target). Time complexity: O(n) MY CODE:
Array Reverse Reverse an array arr[] . Reversing an array means rearranging the elements such that the first element becomes the last , the second element becomes second last and so on. Input: arr[] = [1, 4, 3, 2, 6, 5] Output: [5, 6, 2, 3, 4, 1] Explanation: The first element 1 moves to last position, the second element 4 moves to second-last and so on. Input: arr[] = [4, 5, 1, 2] Output: [2, 1, 5, 4] Explanation: The first element 4 moves to last position, the second element 5 moves to second last and so on. CODE: BRUTE FORCE METHOD: Here, in this method we will create a temporary array to store the reversed given array and then assign it to the given array class Solution : def reverseArray ( arr ): n = len (arr) temp = [ 0 ] * n for i in range (n- 1 ,- 1 ,- 1 ): ...
1. Query all columns for a city in CITY with the ID 1661 . The CITY table is described as follows: Here we make use of the where conditon cllause to retrieve the details of the Id with 1661, we select all the columns to be displayed and "*" is avoided for best practice. SOLUTION: SELECT ID,NAME,COUNTRYCODE,DISTRICT,POPULATION FROM CITY where ID = 1661 ; 2. Query all columns for all American cities in the CITY table with populations larger than 100000 . The CountryCode for America is USA . The CITY table is described as follows: Here we make use of the where condtional clause, to obatin the population to be over thhe given number we make use of the' >' symbol to achieve this. Also the AND operator is used to state two where conditons where we want only the details of cities in USA so we use the countrycode check with 'USA' single quotes are used to check with the value, usage ...
Comments
Post a Comment