written on 26 May 2023
Views : Loading...
ListNode* reverseList(ListNode* head) {
ListNode *cur = head, *prev=NULL,*nxt=NULL;
while(cur!=NULL){
nxt = cur->next;
cur->next = prev;
prev = cur;
cur = nxt;
}
return prev;
}
class Solution {
public:
bool isPalindrome(ListNode* head) {
ListNode *slow = head,*fast = head;
if (head==NULL || head->next==NULL)
return true;
while (fast!=NULL && fast->next!=NULL){
slow = slow->next;
fast = fast->next->next;
}
slow = reverseList(slow);
while(head!=NULL && slow!=NULL){
if (slow->val!=head->val){
return false;
}
slow = slow->next;
head = head->next;
}
return true;
}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *a = headA, *b = headB;
while(a!=b){
if (a==NULL) a = headB;
else a = a->next;
if (b==NULL) b = headA;
else b = b->next;
}
return a;
}
};
Floyd's cycle-finding algorithm / Tortoise and Hare algorithm:
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head==NULL || head->next==NULL) return false;
ListNode *fast = head, *slow = head;
while(fast!=NULL && fast->next!=NULL){
fast = fast->next->next;
slow = slow->next;
if (fast==slow) return true;
}
return false;
}
};
Problem :
You are given an integer array height of length $n$. There are n vertical lines drawn such that the two endpoints of the $i^{th}$ line are $(i, 0)$ and $(i, height[i])$.
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Algorithm:
def maxArea(self, height: List[int]) -> int:
left = 0
right = len(height) - 1
maxArea = 0
while left < right:
currentArea = (right - left) * min(height[left], height[right])
maxArea = max(maxArea, currentArea)
if height[left] < height[right]:
left += 1
else:
right -= 1
return maxArea
Problem : Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
There is only one repeated number in nums, return this repeated number.
You must solve the problem without modifying the array nums and uses only constant extra space.
Algorithm
def find_duplicate(nums):
slow = nums[0]
fast = nums[0]
while True:
slow = nums[slow]
fast = nums[nums[fast]]
if slow == fast:
break
slow = nums[0]
while slow != fast:
slow = nums[slow]
fast = nums[fast]
return slow
Problem:: Sort an array consisting of 0, 1 and 2.
Algorithm:
def DNF_Sort(arr):
left = 0
mid = 0
right = len(arr) - 1
while mid <= right:
if arr[mid] == 0:
arr[mid], arr[left] = arr[left], arr[mid]
mid += 1
left += 1
elif arr[mid] == 1:
mid += 1
elif arr[mid] == 2:
arr[mid], arr[right] = arr[right], arr[mid]
right -= 1
return arr
def remove_nth_from_end(head, n):
dummy = ListNode(0)
dummy.next = head
first = dummy
second = dummy
for _ in range(n):
second = second.next
if not second:
return head.next
while second.next:
first = first.next
second = second.next
first.next = first.next.next
return dummy.next
Rotate an array right by k steps.
def rotate(nums,k):
def reverse_array(nums, start, end):
while start < end:
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1
n = len(nums)
k %= n
# Reverse the entire array
reverse_array(nums, 0, n - 1)
# Reverse the first k elements
reverse_array(nums, 0, k - 1)
# Reverse the remaining n - k elements
reverse_array(nums, k, n - 1)
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
def trap(self, height):
l = 0
r = len(height)-1
ans = 0
left_max = height[l]
right_max = height[r]
while l<r:
if left_max<right_max:
l+=1
lm = max(left_max,height[l])
ans+= left_max - height[l]
else:
r-=1
right_max = max(right_max,height[r])
ans+= right_max - height[r]
return ans