├── C++_Code_Solutions ├── Binary Tree Inorder Traversal.cpp ├── BuubleSort.cpp ├── ContainsDuplicate.cpp ├── ContainsDuplicateII.cpp ├── CreatLinkedListNodes.cpp ├── Delete Node in a Linked List.cpp ├── Find Pivot Index.cpp ├── LearningStack.cpp ├── MergeSort.cpp ├── NodeDelete.cpp ├── RemoveOandEXE.bat ├── Reverse Linked List.cpp ├── Reverse String.cpp ├── Swap Nodes in Pairs.cpp ├── Two_Sum.cpp └── addDigits.cpp ├── Python_Code_Solutions ├── 3Sum.py ├── AddingNodeToStart.py ├── Contains_Duplicate.py ├── Contains_Duplicate_II.py ├── Create_LL_Nodes.py ├── Find Pivot Index.py ├── ReverseString.py ├── Reverse_Linked_List.py ├── SwapNodesInPairs.py └── Two_Sum.py └── README.md /C++_Code_Solutions/Binary Tree Inorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | /* Given a binary tree, return the inorder traversal of its nodes' values. 2 | 3 | Example: 4 | 5 | Input: [1,null,2,3] 6 | 1 7 | \ 8 | 2 9 | / 10 | 3 11 | 12 | Output: [1,3,2] */ 13 | 14 | //Done By >> Anand S Kothari 15 | /** 16 | * Definition for a binary tree node. 17 | * struct TreeNode { 18 | * int val; 19 | * TreeNode *left; 20 | * TreeNode *right; 21 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 22 | * }; 23 | */ 24 | 25 | 26 | class Solution { 27 | public: 28 | vector inorderTraversal(TreeNode* root) { 29 | vector stack; 30 | vector res; 31 | 32 | while(root!=NULL || stack.size()>0) 33 | { 34 | if(root!=NULL) 35 | { 36 | stack.push_back(root); 37 | root = root->left; 38 | }else 39 | { 40 | root = stack.back(); // Go back to the stack value 41 | stack.pop_back(); 42 | res.push_back(root->val); 43 | root = root->right; 44 | 45 | } 46 | } 47 | return res; 48 | } 49 | }; -------------------------------------------------------------------------------- /C++_Code_Solutions/BuubleSort.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace std; 5 | int arr[] = {6,4,2,5,3,5,6,7,1}; 6 | int n = sizeof(arr)/sizeof(arr[0]); 7 | void swapit(int *arr,int a , int b) 8 | { 9 | int temp = arr[a]; 10 | arr[a] = arr[b] ; 11 | arr[b] = temp; 12 | } 13 | 14 | void bubbleSort(int arr[] , int size) 15 | { 16 | for(int i = 0 ; iarr[i+1]) 19 | { 20 | swapit(arr,i,i+1); 21 | bubbleSort(arr,size); 22 | } 23 | } 24 | 25 | 26 | } 27 | 28 | int main() 29 | { 30 | bubbleSort(arr,n); 31 | for (i:arr) 32 | { 33 | cout<> Anand Kothari 21 | Date : 09/25 22 | */ 23 | #include 24 | #include 25 | #include 26 | using namespace std; 27 | 28 | // This has a runtime of O(n), since it will take more time if there is an increase in the input 29 | 30 | class Solution { 31 | public: 32 | bool containsDuplicate(vector& nums) { 33 | unordered_set m; 34 | for(int i=0;i isFound; 52 | ////If there are more duplicates than one and you wan to print all duplicates 53 | // 54 | //class Solution { 55 | //public: 56 | // bool containsDuplicate(vector& nums) { 57 | // unordered_set m; 58 | // for(int i=0;i arr ={1,2,3,4,2,1}; 97 | s.containsDuplicate(arr); 98 | 99 | } 100 | -------------------------------------------------------------------------------- /C++_Code_Solutions/ContainsDuplicateII.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Share 3 | Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. 4 | 5 | Example 1: 6 | 7 | Input: nums = [1,2,3,1], k = 3 8 | Output: true 9 | Example 2: 10 | 11 | Input: nums = [1,0,1,1], k = 1 12 | Output: true 13 | Example 3: 14 | 15 | Input: nums = [1,2,3,1,2,3], k = 2 16 | Output: false 17 | */ 18 | 19 | class Solution { 20 | public: 21 | bool containsNearbyDuplicate(vector& nums, int k) 22 | { 23 | unordered_map map; 24 | 25 | for(int i =0 ; i 3 | using namespace std; 4 | struct Node 5 | { 6 | int data; 7 | Node *next; 8 | }; 9 | 10 | // This function prints contents of linked list starting from 11 | // the given node 12 | void printList( Node *n) 13 | { 14 | while (n != NULL) 15 | { 16 | cout<data<<" "; 17 | n = n->next; 18 | } 19 | } 20 | 21 | int main() 22 | { 23 | Node* head = NULL; 24 | Node* second = NULL; 25 | Node* third = NULL; 26 | 27 | // allocate 3 nodes in the heap 28 | head = ( Node*)malloc(sizeof(struct Node)); 29 | second = ( Node*)malloc(sizeof(struct Node)); 30 | third = ( Node*)malloc(sizeof(struct Node)); 31 | 32 | head->data = 1; //assign data in first node 33 | head->next = second; // Link first node with second 34 | 35 | second->data = 2; //assign data to second node 36 | second->next = third; 37 | 38 | third->data = 3; //assign data to third node 39 | third->next = NULL; 40 | 41 | printList(head); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /C++_Code_Solutions/Delete Node in a Linked List.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. 4 | 5 | Given linked list -- head = [4,5,1,9], which looks like following: 6 | 7 | 4 -> 5 -> 1 -> 9 8 | Example 1: 9 | 10 | Input: head = [4,5,1,9], node = 5 11 | Output: [4,1,9] 12 | Explanation: You are given the second node with value 5, the linked list 13 | should become 4 -> 1 -> 9 after calling your function. 14 | Example 2: 15 | 16 | Input: head = [4,5,1,9], node = 1 17 | Output: [4,5,9] 18 | Explanation: You are given the third node with value 1, the linked list 19 | should become 4 -> 5 -> 9 after calling your function. 20 | Note: 21 | 22 | The linked list will have at least two elements. 23 | All of the nodes' values will be unique. 24 | The given node will not be the tail and it will always be a valid node of the linked list. 25 | Do not return anything from your function. 26 | */ 27 | 28 | // Done By >> Anand Kothari 29 | 30 | #include 31 | 32 | using namespace std; 33 | 34 | struct ListNode { 35 | int val; 36 | ListNode *next; 37 | ListNode(int x) : val(x), next(NULL) {} 38 | }; 39 | 40 | // If say there are five node |2|->|3|->|4|->|5|->|6| 41 | 42 | // Let's say we need to delete Node 3, what the code does is /----| 43 | // Line 1 [stores the value of the node next to 3 into the node 3 position ] :: |2|->|4|->|4|->|5|->|6| 44 | 45 | // Line 2 [Changes the pointer from the second node directly to the fourth node] :: |2|-> |4| -x>|4| -x> |5|->|6| 46 | // |_____________/ 47 | 48 | class Solution{ 49 | public: 50 | void deleteNode(ListNode *node){ 51 | 52 | node->val = node->next->val; 53 | node->next = node->next->next; 54 | 55 | } 56 | 57 | }; 58 | 59 | 60 | -------------------------------------------------------------------------------- /C++_Code_Solutions/Find Pivot Index.cpp: -------------------------------------------------------------------------------- 1 | /* Done By >> Anand Kothari 2 | 3 | Given an array of integers nums, write a method that returns the "pivot" index of this array. 4 | 5 | We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index. 6 | 7 | If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index. 8 | 9 | Example 1: 10 | Input: 11 | nums = [1, 7, 3, 6, 5, 6] 12 | Output: 3 13 | Explanation: 14 | The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3. 15 | Also, 3 is the first index where this occurs. 16 | Example 2: 17 | Input: 18 | nums = [1, 2, 3] 19 | Output: -1 20 | Explanation: 21 | There is no index that satisfies the conditions in the problem statement. 22 | Note: 23 | 24 | The length of nums will be in the range [0, 10000]. 25 | Each element nums[i] will be an integer in the range [-1000, 1000]. 26 | 27 | */ 28 | 29 | class Solution { 30 | public: 31 | int pivotIndex(vector& nums) { 32 | 33 | int n = nums.size(); // Get the total Size of vector array 34 | int sum = accumulate(nums.begin(),nums.end(),0) , currSum = 0 , i = 0; // accumulate is used to sum up values in range 35 | 36 | for (i=0;i 2 | #include 3 | 4 | using namespace std; 5 | stack st; 6 | 7 | 8 | void display(stack ab) 9 | { 10 | while(!ab.empty()) 11 | { 12 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | void merge(int arr[],int s , int e) 7 | { 8 | int mid = (s+e)/2; 9 | int i = s; 10 | int j = mid+1; 11 | int k = s; 12 | 13 | /* create temp arrays */ 14 | int temp[100]; 15 | while(i <= mid && j <= e) 16 | { 17 | // If the first value of left array is less than first value of right value 18 | if(arr[i]=e) 52 | { 53 | return; 54 | } 55 | // Same as (s+e)/2 56 | int mid = s+(e-s)/2; 57 | 58 | mergeSort(arr,s,mid); 59 | mergeSort(arr,mid+1,e); 60 | 61 | merge(arr,s,e); 62 | 63 | } 64 | 65 | 66 | 67 | int main(){ 68 | int arr[] = {2,4,6,3,3,6,2,1}; 69 | int size = sizeof(arr)/sizeof(arr[1]); 70 | int start = 0; 71 | int end = size; 72 | 73 | mergeSort(arr,start,end-1); 74 | 75 | for(int i= 0 ; i> Anand Kothari 2 | // Runtime is O(n) , although it could also be done in O(1) as the delOne function does 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct Node 9 | { 10 | int data; 11 | Node* next; 12 | }; 13 | 14 | void push( Node** head_ref, int new_data) // O(1) :: Runtime 15 | { 16 | //allocate a new_node 17 | Node* new_node =(Node*)malloc(sizeof( Node)); 18 | // Assign Data 19 | new_node->data = new_data; 20 | // Point to current head data 21 | new_node->next = (*head_ref); 22 | // Point head to the newly added node 23 | (*head_ref) = new_node; 24 | 25 | } 26 | 27 | // This function finds for the key which needs to 28 | // be deleted and return nothing if it doesn't have the key 29 | void delNode( Node** head_ref , int key) 30 | { 31 | Node* temp = *head_ref , *prev; 32 | if(temp != NULL && temp->data == key) 33 | { 34 | *head_ref = temp->next; 35 | free(temp); 36 | return; 37 | } 38 | 39 | // We need traverse the entire list for the key 40 | // and also keep track of the previous node 41 | // so that we could change the "prev->next" after deletion 42 | 43 | while(temp !=NULL && temp->data != key) 44 | { 45 | prev = temp; 46 | temp = temp->next; 47 | } 48 | 49 | // If key not found 50 | if(temp == NULL) 51 | { 52 | cout<<" Couldn't find the given node current list is :: " <next = temp->next; 58 | free(temp); // Free memory after deletion 59 | 60 | } 61 | 62 | // This function is to print the updated linked 63 | // list and return the list if no key is found 64 | 65 | void printList(Node* node) // Pass the head if entire list needs to be printed 66 | { 67 | while(node != NULL) 68 | { 69 | cout<data<<" "; 70 | node = node->next; 71 | } 72 | } 73 | 74 | // This runs with O(1) Runtime 75 | void delOne(Node* node) 76 | { 77 | Node* temp = node->next; 78 | node->data = node->next->data; 79 | node->next = temp->next; 80 | free(temp); 81 | } 82 | 83 | int main() 84 | { 85 | Node* head = NULL; 86 | push(&head,8); // Inserts value to the start of the list 87 | push(&head,7); 88 | push(&head,6); 89 | push(&head,5); 90 | push(&head,4); 91 | push(&head,3); 92 | push(&head,2); 93 | push(&head,1); 94 | 95 | cout<<"List is :: " ; 96 | printList(head); 97 | cout<next); // Deletes the node after the first(head) node 102 | printList(head); 103 | 104 | 105 | return 0; 106 | } 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /C++_Code_Solutions/RemoveOandEXE.bat: -------------------------------------------------------------------------------- 1 | C:\Users\DELL\Documents\GitHub\LeetCode_Solutions\C++_Code_Solutions\ 2 | 3 | del /s /f *.o *.exe 4 | -------------------------------------------------------------------------------- /C++_Code_Solutions/Reverse Linked List.cpp: -------------------------------------------------------------------------------- 1 | /* Reverse a singly linked list. 2 | 3 | Example: 4 | 5 | Input: 1->2->3->4->5->NULL 6 | Output: 5->4->3->2->1->NULL 7 | Follow up: 8 | 9 | A linked list can be reversed either iteratively or recursively. Could you implement both? 10 | */ 11 | 12 | // Done by >> Anand Kothari 13 | 14 | /* 15 | Definition for singly-linked list. 16 | struct ListNode { 17 | int val; 18 | ListNode *next; 19 | ListNode(int x) : val(x), next(NULL) {} 20 | }; 21 | */ 22 | class Solution { 23 | public: 24 | ListNode* reverseList(ListNode* head) { 25 | 26 | // RECURSION METHOD 27 | 28 | // if (head==NULL || head->next==NULL) return head; 29 | // ListNode *h = reverseList(head->next); 30 | // head->next->next = head; 31 | // head->next = NULL; 32 | // return h; 33 | 34 | 35 | // Iterative method 36 | ListNode *prev = NULL , *current = head, *next = NULL; 37 | 38 | while(current != NULL) 39 | { 40 | // Store next 41 | next = current->next; 42 | 43 | // Reverse current node's pointer 44 | current->next = prev; 45 | 46 | // Move pointers one position ahead. 47 | prev = current; 48 | current = next; 49 | } 50 | head = prev ; 51 | return head; 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /C++_Code_Solutions/Reverse String.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a function that takes a string as input and returns the string reversed. 3 | 4 | Example 1: 5 | 6 | Input: "hello" 7 | Output: "olleh" 8 | Example 2: 9 | 10 | Input: "A man, a plan, a canal: Panama" 11 | Output: "amanaP :lanac a ,nalp a ,nam A" 12 | */ 13 | 14 | //Done by >> Anand Kothari 15 | class Solution { 16 | public: 17 | string reverseString(string s) { 18 | 19 | int len = s.size(); 20 | char temp; 21 | 22 | for(int i=0 ; i2->3->4, you should return the list as 2->1->4->3. 8 | Note: 9 | 10 | Your algorithm should use only constant extra space. 11 | You may not modify the values in the list's nodes, only nodes itself may be changed. 12 | Done by >> Anand Kothari 13 | */ 14 | 15 | /** 16 | * Definition for singly-linked list. 17 | * struct ListNode { 18 | * int val; 19 | * ListNode *next; 20 | * ListNode(int x) : val(x), next(NULL) {} 21 | * }; 22 | */ 23 | class Solution { 24 | public: 25 | ListNode* swapPairs(ListNode* head) { 26 | // Will Run till it has pairs and would ignore the single digits at the end 27 | for( ListNode *p = head; p && p->next ;p = p->next->next) 28 | { 29 | int n = p->val; 30 | p->val = p->next->val; 31 | p->next->val = n; 32 | } 33 | return head; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /C++_Code_Solutions/Two_Sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of integers, return indices of the two numbers such that they add up to a specific target. 3 | 4 | You may assume that each input would have exactly one solution, and you may not use the same element twice. 5 | 6 | Example: 7 | 8 | Given nums = [2, 7, 11, 15], target = 9, 9 | 10 | Because nums[0] + nums[1] = 2 + 7 = 9, 11 | return [0, 1]. */ 12 | 13 | 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | vector result; 19 | 20 | /* CODE TO GET VALUES WITHOUT THE USE OF VECTOR 21 | 22 | class Solution { 23 | public: 24 | int twoSum(int nums[], int target) { 25 | 26 | for(int i=0;i twoSum(vector &numbers, int target) { 61 | unordered_map comp; // The syntax is kinda like this unordered_map variable_name; 62 | vector result; 63 | for(int i=0; i arr = {2,7,11,15}; 83 | result = s.twoSum(arr,9); 84 | for(int value:result) // Advanced for loop read as "For each value in result do this" 85 | { 86 | cout<< result[value]; 87 | } 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /C++_Code_Solutions/addDigits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 3 | 4 | Example: 5 | 6 | Input: 38 7 | Output: 2 8 | Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2. 9 | Since 2 has only one digit, return it. 10 | Follow up: 11 | Could you do it without any loop/recursion in O(1) runtime? 12 | 13 | 14 | By >> Anand Kothari 15 | 16 | */ 17 | 18 | #include 19 | 20 | using namespace std; 21 | 22 | class Solution { 23 | public: 24 | 25 | int addDigits(int num) { 26 | 27 | switch(rand()%5+1){ // It gives range 0..5 therefore added one to get 1..6 28 | case 1: return addDigits01(num); 29 | case 2: return addDigits02(num); 30 | case 3: return addDigits03(num); 31 | case 4: return addDigits04(num); 32 | default: return addDigits05(num); 33 | } 34 | 35 | } 36 | 37 | //regualr way 38 | int addDigits01(int num) { 39 | while(num > 9) { 40 | int sum; 41 | for(sum=0; num > 0; sum += num%10 , num/=10); 42 | num = sum; 43 | } 44 | return num; 45 | 46 | } 47 | 48 | //This solution looks is very tricky, but actually it is easy to understand. 49 | //it just keep adding the last digital until the num < 10 50 | int addDigits02(int num) { 51 | while(num > 9) { 52 | num = num / 10 + num % 10; 53 | } 54 | return num; 55 | 56 | } 57 | 58 | // Let's observe the pattern 59 | // 1 1 60 | // 2 2 61 | // ... ... 62 | // 8 8 63 | // 9 9 64 | // 10 1 65 | // 11 2 66 | // 12 3 67 | // ... ... 68 | // 17 8 69 | // 18 9 70 | // 19 1 71 | // 20 2 72 | // ... ... 73 | // It looks most of number just simply %9 is the answer, 74 | // but there are some edge cases. 75 | // 9%9=0 but we need 9. 76 | // 18%9=0 but we need 9 77 | // so we can find the solution is: 78 | // 1) num <=9, return num 79 | // 2) num > 9, reutrn num%9 if num%9>0 80 | // return 9 if num%9 ==0 81 | int addDigits03(int num) { 82 | return num >9 ? ((num %9)==0 ? 9:num%9) : num; 83 | } 84 | 85 | //But actually, we can use (num-1)%9 + 1 to make all cases right. 86 | int addDigits04(int num){ 87 | return (num - 1) % 9 + 1; 88 | } 89 | 90 | //This solution is similar with pervious solution. 91 | int addDigits05(int num){ 92 | return num - 9 * ((num - 1)/9); 93 | } 94 | 95 | 96 | }; 97 | 98 | int main() 99 | { 100 | Solution s; 101 | int p= s.addDigits(38); 102 | cout< 0 and nums[i] == nums[i-1]: # This says that if the previos value of 'i' is same as the current value than the result would be same therefore skip that duplicate result 32 | continue # It skips the current loop and continues with the conditional while loop 33 | j = i+1 34 | k = length_of_nums - 1 35 | 36 | while j < k: 37 | current_Summation = nums[i] + nums[j] + nums[k] 38 | if current_Summation == 0: 39 | result.append([nums[i],nums[j],nums[k]]) 40 | while j Anand Kothari 3 | ''' 4 | class Node: 5 | def __init__(self,value=None): 6 | self.value = value 7 | self.nextvalue = None 8 | 9 | class linkedList: 10 | def __init__(self): 11 | self.head = None 12 | 13 | # Print the linked list 14 | def printList(self): 15 | thisvalue = self.head 16 | while thisvalue: 17 | print(thisvalue.value) 18 | thisvalue = thisvalue.nextvalue 19 | 20 | def inertAtBegining(self,newValue): 21 | newData = Node(newValue) 22 | newData.nextvalue = self.head # Update the new nodes next val to existing node 23 | self.head = newData 24 | 25 | 26 | 27 | list = linkedList() 28 | list.head = Node("Mon") 29 | list2 = Node("Tue") 30 | list3 = Node("Wed") 31 | 32 | list.head.nextvalue = list2 33 | list2.nextvalue = list3 34 | print(''' 35 | Before Adding a Node: 36 | ''' 37 | ) 38 | list.printList() 39 | 40 | print(''' 41 | After adding a Node in the start: 42 | ''') 43 | list.inertAtBegining("Sun") # This Node will be added in the start 44 | list.printList() 45 | -------------------------------------------------------------------------------- /Python_Code_Solutions/Contains_Duplicate.py: -------------------------------------------------------------------------------- 1 | """ 2 | BY: 'Anand Kothai' ; 3 | Given an array of integers, find if the array contains any duplicates. 4 | 5 | Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 6 | 7 | Example 1: 8 | 9 | Input: [1,2,3,1] 10 | Output: true 11 | Example 2: 12 | 13 | Input: [1,2,3,4] 14 | Output: false 15 | """ 16 | 17 | 18 | class Solution(object): 19 | def containsDuplicate(self, nums): 20 | """ 21 | :type nums: List[int] 22 | :rtype: bool 23 | """ 24 | 25 | return (len(nums) != len(set(nums))) # Set function removes the duplicates and then len function is used to check if the length of nums is same also after removing the duplicates 26 | 27 | if __name__ == '__main__': 28 | s=Solution() 29 | print(s.containsDuplicate([1,2,3,1])) 30 | -------------------------------------------------------------------------------- /Python_Code_Solutions/Contains_Duplicate_II.py: -------------------------------------------------------------------------------- 1 | """ 2 | Done by >> Anand Kothari 3 | Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. 4 | 5 | Example 1: 6 | 7 | Input: nums = [1,2,3,1], k = 3 8 | Output: true 9 | Example 2: 10 | 11 | Input: nums = [1,0,1,1], k = 1 12 | Output: true 13 | Example 3: 14 | 15 | Input: nums = [1,2,3,1,2,3], k = 2 16 | Output: false """ 17 | 18 | class Solution(object): 19 | def containsNearbyDuplicate(self, nums, k): 20 | """ 21 | :type nums: List[int] 22 | :type k: int 23 | :rtype: bool 24 | """ 25 | check = set() 26 | for i in range(len(nums)): 27 | if i > k: 28 | check.remove(nums[i - k - 1]) 29 | if nums[i] in check: 30 | return True 31 | else: 32 | check.add(nums[i]) 33 | return False 34 | 35 | 36 | 37 | if __name__ == '__main__': 38 | s= Solution() 39 | Result= s.containsNearbyDuplicate([1,2,3,1,2,3],2) 40 | if Result != True: 41 | print (False) 42 | else: 43 | print (True) 44 | -------------------------------------------------------------------------------- /Python_Code_Solutions/Create_LL_Nodes.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Done By -> Anand Kothari 3 | The nodes are created by implementing a class which will hold the pointers along with the data element. 4 | ''' 5 | 6 | 7 | class dayname: 8 | def __init__(self, dataval=None): 9 | self.dataval = dataval # To Store value of a nodes 10 | self.nextval = None # Pointer is initialized to null 11 | 12 | 13 | d1 = dayname('Mon') 14 | d2 = dayname('Wed') 15 | d3 = dayname('Thur') 16 | d4 = dayname('Tue') 17 | 18 | #The nextval pointer of node d1 points to d4 while the nextval pointer of node d4 points to d2 and so on. 19 | 20 | d1.nextval = d4 21 | d4.nextval = d2 22 | d2.nextval = d3 23 | 24 | # We have created the nodes now and also created pointers 25 | 26 | # Lets TRAVERSE 27 | # Consider "thisvalue" as the head pointer [Start of the list of nodes, concept of linked lists], it will be used to traverse the node elements 28 | thisvalue = d1 29 | 30 | while thisvalue: 31 | print(thisvalue.dataval) 32 | thisvalue = thisvalue.nextval 33 | ''' 34 | Output: 35 | ------- 36 | 37 | Mon 38 | Tue 39 | Wed 40 | Thur 41 | 42 | ''' -------------------------------------------------------------------------------- /Python_Code_Solutions/Find Pivot Index.py: -------------------------------------------------------------------------------- 1 | """ 2 | Done By >> Anand Kothari 3 | 4 | 5 | Given an array of integers nums, write a method that returns the "pivot" index of this array. 6 | 7 | We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index. 8 | 9 | If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index. 10 | 11 | Example 1: 12 | Input: 13 | nums = [1, 7, 3, 6, 5, 6] 14 | Output: 3 15 | Explanation: 16 | The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3. 17 | Also, 3 is the first index where this occurs. 18 | Example 2: 19 | Input: 20 | nums = [1, 2, 3] 21 | Output: -1 22 | Explanation: 23 | There is no index that satisfies the conditions in the problem statement. 24 | 25 | Note: 26 | The length of nums will be in the range [0, 10000]. 27 | Each element nums[i] will be an integer in the range [-1000, 1000]. 28 | 29 | """ 30 | 31 | class Solution(object): 32 | def pivotIndex(self, nums): 33 | n = len(nums) # Gets Length of the list 34 | sums = sum(nums) # Gets total of the list values 35 | currSum = 0 36 | 37 | 38 | for i in range(n): 39 | if sums-nums[i] == 2*currSum: #Compares left sum of the index with the right 40 | return i 41 | else: 42 | currSum+= nums[i] # Stores the sum of left values of the Index so that we don't have to loop through it again and again to find the sum 43 | return -1 44 | -------------------------------------------------------------------------------- /Python_Code_Solutions/ReverseString.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def reverseString(self, s): 3 | return(s[::-1]) 4 | -------------------------------------------------------------------------------- /Python_Code_Solutions/Reverse_Linked_List.py: -------------------------------------------------------------------------------- 1 | 2 | ''' 3 | Done By > Anand Kothari 4 | 5 | Question:: 6 | Reverse a singly linked list. 7 | 8 | Example: 9 | 10 | Input: 1->2->3->4->5->NULL 11 | Output: 5->4->3->2->1->NULL 12 | 13 | -------------------------------------- 14 | Solution: 15 | 16 | ''' 17 | # Definition for singly-linked list. 18 | # class ListNode(object): 19 | # def __init__(self, x): 20 | # self.val = x 21 | # self.next = None 22 | 23 | # Simple iteratively without extra space 24 | 25 | # class Solution(object): 26 | # def reverseList(self, head): 27 | # prev = None 28 | # curr = head 29 | # while curr!=None : 30 | # temp = curr.next 31 | # curr.next = prev 32 | # prev = curr 33 | # curr = temp 34 | # head = prev 35 | # return (head) 36 | 37 | # Recursively 38 | 39 | class Solution: 40 | def reverseList(self, head): 41 | 42 | if head is None or head.next is None: 43 | return head 44 | 45 | p = self.reverseList(head.next) 46 | head.next.next= head 47 | head.next = None # It removes the unwanted pointer 48 | return p 49 | -------------------------------------------------------------------------------- /Python_Code_Solutions/SwapNodesInPairs.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Done By -> Anand Kothari 3 | 4 | Question:: 5 | 6 | Given a linked list, swap every two adjacent nodes and return its head. 7 | 8 | Example: 9 | 10 | Given 1->2->3->4, you should return the list as 2->1->4->3. 11 | Note: 12 | 13 | Your algorithm should use only constant extra space. 14 | You may not modify the values in the list's nodes, only nodes itself may be changed. 15 | 16 | 17 | Solution: 18 | 19 | ''' 20 | 21 | 22 | # Definition for singly-linked list. 23 | # class ListNode(object): 24 | # def __init__(self, x): 25 | # self.val = x 26 | # self.next = None 27 | 28 | class Solution(object): 29 | def swapPairs(self, head): 30 | fakeNode = ListNode(-1) 31 | fakeNode.next = head 32 | 33 | prev , p = fakeNode , head 34 | while p!= None and p.next!=None: 35 | q = p.next 36 | r = p.next.next 37 | q.next = p 38 | p.next = r 39 | prev.next = q 40 | prev = p 41 | p = r 42 | return fakeNode.next 43 | -------------------------------------------------------------------------------- /Python_Code_Solutions/Two_Sum.py: -------------------------------------------------------------------------------- 1 | """ 2 | By: Anand S Kothari 3 | 4 | * Given an array of integers, find two numbers such that they add up to a specific target number. 5 | * 6 | * The function twoSum should return indices of the two numbers such that they add up to the target, 7 | * where index1 must be less than index2. Please note that your returned answers (both index1 and index2) 8 | * are not zero-based. 9 | * 10 | * You may assume that each input would have exactly one solution. 11 | * 12 | * Input: numbers={2, 7, 11, 15}, target=9 13 | * Output: index1=1, index2=2 14 | """ 15 | 16 | # The easy solution is O(n^2) run-time complexity. 17 | class Solution(object): 18 | 19 | # def twoSum(self, nums, target ): #Degining the twoSum function 20 | # length_of_nums = len(nums) # Storing len of the input array 21 | # for i in range(length_of_nums): # Storing range of the length which starts from 0 22 | # for j in range(i+1, length_of_nums): #Storing range from the i+1 position 23 | # if (nums[i] + nums[j] == target): #Condition 24 | # return [i,j] # Return Statement 25 | # 26 | 27 | def twoSum(self, nums, target): 28 | # hash 2 29 | hash_nums = {} 30 | for index, num in enumerate(nums): 31 | another = target - num 32 | try: 33 | hash_nums[another] 34 | return [hash_nums[another], index] 35 | except KeyError: 36 | hash_nums[num] = index 37 | 38 | 39 | if __name__ == '__main__': 40 | s = Solution() 41 | print (s.twoSum([2, 7,11, 15], 9)) 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode_Solutions 2 | Solutions of LeetCode [interview](http://www.learn4master.com/interview-questions/leetcode/leetcode-problems-classified-by-company) questions 3 | 4 | | # | Title | Solution | Difficulty | Appeared in Interviews of | 5 | |---|:-----:|:--------:| :---------:| :---------:| 6 | | 1 | [Two_Sum](https://leetcode.com/problems/two-sum/description/) | [ Python ](https://github.com/kotharan/LeetCode_Solutions/blob/master/Python_Code_Solutions/Two_Sum.py)
[C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/Two_Sum.cpp)| Medium | Airbnb , Facebook , Amazon , Microsoft , Apple ,
Yahoo , Dropbox , Bloomberg , Yelp , Adobe , LinkedIn | 7 | | 15 | [3Sum](https://leetcode.com/problems/3sum/description/) | [ Python ](https://github.com/kotharan/LeetCode_Solutions/blob/master/Python_Code_Solutions/3Sum.py)| Medium | Facebook , Google , Amazon , Microsoft , Boomberg , Adobe | 8 | | 24 |[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/description/) | [C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/Swap%20Nodes%20in%20Pairs.cpp)
[Python](https://github.com/kotharan/LeetCode_Solutions/blob/master/Python_Code_Solutions/SwapNodesInPairs.py) | Medium | Bloomberg , Microsoft , Uber | 9 | | 94 |[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/description/) | [C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/Binary%20Tree%20Inorder%20Traversal.cpp) | Medium | Microsoft | 10 | | 206 |[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/) | [C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/Reverse%20Linked%20List.cpp)
[Python](https://github.com/kotharan/LeetCode_Solutions/blob/master/Python_Code_Solutions/Reverse_Linked_List.py)| Easy | Amazon , Apple , Adobe , Bolomberg , Facebook , Microsoft, Snapchat , Twitter , Uber , Yahoo , Yelp , Zenefit | 11 | | 217 | [Contains Duplicate ](https://leetcode.com/problems/contains-duplicate/description/) | [ Python ](https://github.com/kotharan/LeetCode_Solutions/blob/master/Python_Code_Solutions/Contains_Duplicate.py)
[C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/ContainsDuplicate.cpp)| Easy | Palantir , Airbnb , Yahoo | 12 | | 219 | [Contains Duplicate II ](https://leetcode.com/problems/contains-duplicate-ii/description/) | [ Python ](https://github.com/kotharan/LeetCode_Solutions/blob/master/Python_Code_Solutions/Contains_Duplicate_II.py)
[C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/ContainsDuplicateII.cpp)| Easy | Palantir , Airbnb , Yahoo | 13 | | 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/description/) | [C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/Delete%20Node%20in%20a%20Linked%20List.cpp) | Easy | Apple , Adobe , Microsoft | 14 | | 258 | [Add Digits](https://leetcode.com/problems/add-digits/description/) | [C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/addDigits.cpp) | Easy | Microsoft , Adobe | 15 | | 344 | [Reverse String](https://leetcode.com/problems/reverse-string/description/) | [C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/Reverse%20String.cpp)
[Python](https://github.com/kotharan/LeetCode_Solutions/blob/master/Python_Code_Solutions/ReverseString.py)| Easy | Not Appeared yet | 16 | | 724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/description/) | [C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/Find%20Pivot%20Index.cpp) [Python](https://github.com/kotharan/LeetCode_Solutions/blob/master/Python_Code_Solutions/Find%20Pivot%20Index.py) | Easy | Not Appeared yet | 17 | 18 | | # | Important codes you should know | Solution | Description | 19 | |---|:---------:| :---------:| :---------:| 20 | | 1 | How to create Linked list nodes | [C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/CreatLinkedListNodes.cpp)
[Python](https://github.com/kotharan/LeetCode_Solutions/blob/master/Python_Code_Solutions/Create_LL_Nodes.py) | Creating three nodes and printing them | 21 | | 2 | Add a Node at the start | [Python](https://github.com/kotharan/LeetCode_Solutions/blob/master/Python_Code_Solutions/AddingNodeToStart.py) | Creating nodes and adding node at the start | 22 | | 3 | How to delete Linked list nodes with the key | [C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/NodeDelete.cpp) | With O(n) and O(1) runtime functions | 23 | | 4 | Bubble Sort with Recursion | [C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/BuubleSort.cpp) | With O(n) | 24 | | 5 | Merge Sort with Recursion | [C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/MergeSort.cpp) | O(n log n) | 25 | | 6 | Stack Basics | [C++](https://github.com/kotharan/LeetCode_Solutions/blob/master/C%2B%2B_Code_Solutions/LearningStack.cpp) | Learn about LIFO | 26 | --------------------------------------------------------------------------------