Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

leetcode day1

The document contains multiple Java class implementations for solving various algorithmic problems, including merging sorted arrays, merging two linked lists, deleting duplicates from a linked list, detecting cycles in a linked list, checking for palindromes, removing the nth node from the end of a linked list, validating parentheses, moving zeros to the end of an array, and finding a missing number in an array. Each class provides a specific solution using methods and data structures appropriate for the problem at hand. The solutions demonstrate a variety of techniques including recursion, iteration, and the use of stacks.

Uploaded by

Harsh Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

leetcode day1

The document contains multiple Java class implementations for solving various algorithmic problems, including merging sorted arrays, merging two linked lists, deleting duplicates from a linked list, detecting cycles in a linked list, checking for palindromes, removing the nth node from the end of a linked list, validating parentheses, moving zeros to the end of an array, and finding a missing number in an array. Each class provides a specific solution using methods and data structures appropriate for the problem at hand. The solutions demonstrate a variety of techniques including recursion, iteration, and the use of stacks.

Uploaded by

Harsh Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

class Solution {

public void merge(int[] nums1, int m, int[] nums2, int n) {


//variables to work as pointers
int i=m-1; // will point at m-1 index of nums1 array
int j=n-1; // will point at n-1 index of nums2 array
int k=nums1.length-1; //will point at the last position of the nums1 array

// Now traversing the nums2 array


while(j>=0){
// If element at i index of nums1 > element at j index of nums2
// then it is largest among two arrays and will be stored at k position
of nums1
// using i>=0 to make sure we have elements to compare in nums1 array
if(i>=0 && nums1[i]>nums2[j]){
nums1[k]=nums1[i];
k--;
i--; //updating pointer for further comparisons
}else{
// element at j index of nums2 array is greater than the element at
i index of nums1 array
// or there is no element left to compare with the nums1 array
// and we just have to push the elements of nums2 array in the
nums1 array.
nums1[k] = nums2[j];
k--;
j--; //updating pointer for further comparisons
}
}
}
}

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1!=null && list2!=null){
if(list1.val<list2.val){
list1.next=mergeTwoLists(list1.next,list2);
return list1;
}
else{
list2.next =mergeTwoLists(list1,list2.next);
return list2;
}
}
if(list1==null)
return list2;
else
return list1;
}
}

class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode currNode =head;
while(currNode!= null && currNode.next!=null){
if(currNode.val==currNode.next.val){
currNode.next =currNode.next.next;
}
else{
currNode =currNode.next;
}
}
return head;
}
}

public class Solution {


public boolean hasCycle(ListNode head) {
ListNode temp= head;
ListNode temp2=head;
while(temp!=null && temp.next !=null){
temp2 =temp2.next;
temp=temp.next.next;
if(temp2 == temp){
return true;
}
}
return false;
}
}

//Valid Palindrome
class Solution {
public boolean isPalindrome(String s) {
if(s.isEmpty()){
return true;
}
int start=0;
int last=s.length()-1;
while(start<=last){
char currFirst=s.charAt(start);
char currLast=s.charAt(last);
if(!Character.isLetterOrDigit(currFirst)){
start++;
}
else if(!Character.isLetterOrDigit(currLast)){
last--;
}
else{
if(Character.toLowerCase(currFirst) != Character.toLowerCase
(currLast)){
return false;
}
start++;
last--;
}
}
return true;
}
}
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head.next == null){
return null;
}
int size=0;
ListNode temp =head;
while(temp != null ){
temp=temp.next;
size++;
}
//removing Sizeth node from list i.e head
if(n == size){
return head.next;
}
//find previous node
int pstfind = size -n; //position to find
ListNode prev = head; //previous node

int currp = 1; // currrent position

while(currp != pstfind){
prev =prev.next;
currp++;
}
prev.next =prev.next.next;
return head;
}
}

//valid parenthesis:

import java.util.*;
class Solution {
public boolean isValid(String s) {
Stack<Character> stack= new Stack<>();
for(int i=0 ; i<s.length();i++){
char ch =s.charAt(i);

if(ch =='(' || ch== '{' || ch== '['){


stack.push(ch);
}
else{
if(stack.isEmpty()){
return false;
}

if( stack.peek() == '(' && ch==')'|| stack.peek()=='{' && ch=='}'||


stack.peek()== '[' && ch==']'){
stack.pop();
}
else{
return false;
}
}
}

if(stack.isEmpty()){
return true;
}
else{
return false;
}
}

//Move zeros at last


class Solution {
public void moveZeroes(int[] nums) {
int writer = 0;

for(int reader = 0; reader < nums.length; reader++){


if(nums[reader] != 0){
nums[writer] = nums[reader];
writer++;
}
}

while(writer < nums.length){


nums[writer] = 0;
writer++;
}
}
}

//Missing Number

class Solution {
public int missingNumber(int[]nums) {
//Step1: Find the length of array.
int n = nums.length;

//Step2: Calculate the actual sum using Mathematical formulae


long actualSum = (n*(n+1))/2;

//Step3: Calculate the given sum.


long sum = 0;
for(int element: nums)
{
sum += element;
}

//Step 4: return actualSum-givenSum.


return (int)(actualSum-sum);
}
}
class Solution {
public int missingNumber(int[] nums) {
int allXOR = 0;
for(int i =0; i<=nums.length;i++){
allXOR = allXOR ^ i;
}

for(int num : nums){


allXOR = allXOR ^ num;
}

return allXOR;
}
}

You might also like