Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 579 of 2109
Intersection of Two Linked Lists in Python
When two linked lists share common nodes, we need to find their intersection point. The intersection occurs where both lists reference the same node object, not just nodes with the same value. Given two linked lists A and B, we return the reference to the first common node. If there's no intersection, we return None. Algorithm We use a hash map to track visited nodes from the first list, then check if any node from the second list exists in our map ? Create a dictionary to store nodes from list A Traverse list A ...
Read MoreMin Stack in Python
A Min Stack is a special data structure that supports all standard stack operations (push, pop, top) plus retrieving the minimum element in constant O(1) time. This is achieved by storing the previous minimum value alongside each new minimum. Algorithm The key insight is to store the previous minimum when pushing a new minimum value ? Initialize the stack with min element as infinity For push(x): if x ≤ current min, store the old min in stack first, then update min to x For pop(): if popped element equals current min, restore the previous min from ...
Read MoreLinked List Cycle in Python
A linked list cycle occurs when a node in the linked list points back to a previous node, creating a loop. To detect cycles, we can use a hash set to track visited nodes or implement Floyd's cycle detection algorithm (tortoise and hare). The cycle is represented by a pos parameter indicating where the tail connects. If pos = -1, no cycle exists. For example, in the list [5, 3, 2, 0, -4, 7] with pos = 1, the tail connects to the second node (index 1), creating a cycle. Using Hash Set Approach This method tracks ...
Read MoreHamming Distance in Python
The Hamming distance between two integers is the number of positions where the corresponding bits differ. For example, if we have numbers 7 and 15, they are 0111 and 1111 in binary respectively. Only the most significant bit differs, so the Hamming distance is 1. Algorithm Steps To calculate the Hamming distance, we follow these steps ? Iterate through each bit position from 31 down to 0 Extract the bit at position i for both numbers using right shift and bitwise AND Compare the bits ? if they differ, increment the distance counter Return the total ...
Read MoreFizz Buzz in Python
The Fizz Buzz problem is a classic programming challenge where we replace numbers with specific strings based on divisibility rules. For numbers from 1 to n, we print "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both. Problem Rules If the number is divisible by 3, write "Fizz" instead of the number If the number is divisible by 5, write "Buzz" instead of the number If the number is divisible by both 3 and 5, write "FizzBuzz" instead ...
Read MoreMove Zeroes in Python
Sometimes we need to move all zero values in an array to the right while maintaining the relative order of non-zero elements. For example, transforming [0, 1, 5, 0, 3, 8, 0, 0, 9] into [1, 5, 3, 8, 9, 0, 0, 0, 0]. Algorithm Approach We use a two-pointer technique to solve this problem efficiently ? Use an insert_index pointer to track where to place the next non-zero element Iterate through the array and move non-zero elements to the front Fill remaining positions ...
Read MoreMissing Number in Python
Suppose we have a list of numbers from 0 to n. There is one number that is missing. We have to find the missing number in an efficient approach. So if A = [0, 1, 2, 3, 4, 5, 7, 8, 9], missing number is 6. Python provides multiple approaches to solve this problem efficiently. We'll explore three common methods: binary search, mathematical formula, and XOR operation. Method 1: Using Binary Search The binary search approach works by comparing array values with their indices after sorting ? Sort the list in ascending order Set high ...
Read MoreValid Anagram in Python
Anagrams are words or phrases formed by rearranging the letters of another word or phrase, using all the original letters exactly once. For example, "ANAGRAM" and "NAAGARM" are anagrams because they contain the same letters in different arrangements, but "cat" and "fat" are not anagrams because they have different letters. To check if two strings are valid anagrams, we can use several approaches. The most common method is to sort the characters of both strings and compare them. Method 1: Using Sorting Convert both strings to sorted character lists and compare them ? def is_anagram_sorting(s, ...
Read MoreDelete Node in a Linked List in Python
A linked list is a linear data structure where each element is a separate object, commonly referred to as a node. Each node contains two fields: the data and a pointer to the next node in the list. In this article, we'll learn how to delete a node from a singly linked list using Python. Suppose we have a linked list with a few elements. Our task is to delete a specific node, given only access to that node − not the head of the list ? Input: 1 → 3 → 5 → 7 → ...
Read MoreInvert Binary Tree in Python
Inverting a binary tree means swapping the left and right child nodes for every node in the tree. This creates a mirror image of the original tree structure. Original Tree 4 2 7 1 3 6 ...
Read More