Posts

Showing posts from August, 2024

LinkedList:Remove Duplicates from Sorted List

  Approach Traverse the List : Iterate through the linked list. Check for Duplicates : Compare the current node with the next node. Remove Duplicates : If duplicates are found, adjust pointers to remove the duplicate nodes. Continue : Move to the next node and repeat until the end of the list. Explanation ListNode Struct : This defines the structure for the linked list nodes. removeDuplicates Function : Traverse the list with a pointer current . If current->val is the same as current->next->val , then adjust pointers to remove the duplicate node and free its memory. If no duplicate is found, move current to the next node. Helper Functions : printList is used to display the linked list. createNode is a convenience function to create new nodes. Main Function : Creates a sample sorted linked list with duplicates. Prints the list before and after removing duplicates. Cleans up allocated memory. This method efficiently removes duplicates from a sorted linked list in linear ...

Two Sum : of Array Element thats equal to target sume

Save the index of an element to a hashmap that if deduction from target is not equal to already substracted element. class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> imap(nums.size()); for( int i=0; i<nums.size(); ++i) { if(imap.find(target-nums[i]) == imap.end()) { imap[nums[i]] = i; } else return {imap[target-nums[i]], i}; } return {0,0}; } };

Add Two Numbers without using '+' operator : C++

To perform addition using bitwise operators, you need to use a combination of bitwise AND, XOR, and shift operations. Here's a step-by-step explanation of how this works:  Explanation 1. Bitwise XOR (`^`): This operation is used to add two bits without carrying. For example, `1 ^ 1 = 0` (with carry), and `0 ^ 1 = 1`. 2. Bitwise AND (`&`): This operation is used to determine which bits will need to be carried. For example, `1 & 1 = 1` (indicating a carry is needed), and `0 & 1 = 0` (no carry). 3. Left Shift (`<<`): This operation is used to shift the carry to the left by one position, so it can be added in the next higher bit position.  Steps to Add Two Numbers 1. Calculate Partial Sum: Use XOR to get the partial sum without considering the carry. 2. Calculate Carry: Use AND and then shift left to find the carry for the next bit. 3. Repeat: Continue the process until there is no carry left.  Example Code in Pyt...

LinkedList: Add Two Numbers - C++

Run main look till list1 and list2 both of them reaches end and carry of two digit some is zero. keep adding carry to the sum.  #code - cpp - C++ ```cpp [] /**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     ListNode *next;  *     ListNode() : val(0), next(nullptr) {}  *     ListNode(int x) : val(x), next(nullptr) {}  *     ListNode(int x, ListNode *next) : val(x), next(next) {}  * };  */ class Solution { public:     int add(ListNode* l1, ListNode* l2, int&amp; carry){         int sum = carry;         if(l1)             sum += l1-&gt;val;         if(l2)              sum += l2-&gt;val;                  carry = sum / 10;       ...