Found 10476 Articles for Python

Linked List Cycle in Python

Arnab Chakraborty
Updated on 28-Apr-2020 13:22:38

2K+ Views

Consider we have a linked list, and we have to check whether there is any cycle or not. To represent the cycle in the given linked list, we will use one integer pointer called pos. This pos represents a position in the linked list where tail is connected. So if pos is -1, then there is no cycle present in the linked list. For example, the linked list is like [5, 3, 2, 0, -4, 7], and pos = 1. So there is a cycle, and tail is connected to the second node.To solve this, we will follow these steps ... Read More

Hamming Distance in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:05:39

4K+ Views

Consider we have two integers. We have to find the Hamming distance of them. The hamming distance is the number of bit different bit count between two numbers. So if the numbers are 7 and 15, they are 0111 and 1111 in binary, here the MSb is different, so the Hamming distance is 1.To solve this, we will follow these steps −For i = 31 down to 0b1 = right shift of x (i AND 1 time)b2 = right shift of y (i AND 1 time)if b1 = b2, then answer := answer + 0, otherwise answer := answer + ... Read More

Fizz Buzz in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:03:59

4K+ Views

Suppose we have a number n. We have to display a string representation of all numbers from 1 to n, but there are some constraints.If the number is divisible by 3, write Fizz instead of the numberIf the number is divisible by 5, write Buzz instead of the numberIf the number is divisible by 3 and 5 both, write FizzBuzz instead of the numberTo solve this, we will follow these steps −For all number from 1 to n, if a number is divisible by 3 and 5 both, print “FizzBuzz”otherwise when the number is divisible by 3, print “Fizz”otherwise when ... Read More

Move Zeroes in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:46:18

1K+ Views

Suppose we have an array to hold some numbers. There are non-zero values as well as zero values. So we have to send all zeros to the right without changing the relative order of other numbers. So if the array is like [0, 1, 5, 0, 3, 8, 0, 0, 9], then the final array will be [1, 5, 3, 8, 9, 0, 0, 0, 0]To solve this, we will follow these steps −Suppose index = 0for i = 0 to the length of Aif A[i] != 0, thenA[index] := A[i]index := index + 1for i = index to the ... Read More

Missing Number in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:43:48

437 Views

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.To solve this, we will use the binary search approach.sort the list in ascending orderhigh = length of A, and low = 0while low < high, domid = low + (high – low)/2if A[mid] > midhigh = midotherwiselow = mid + 1return lowExampleLet us see the following implementation to get a better understanding − Live Democlass ... Read More

Valid Anagram in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:33:32

645 Views

Anagrams are basically all permutations of a given string or pattern. This pattern searching algorithm is slightly different. In this case, not only the exact pattern is searched, it searches all possible arrangements of the given pattern in the text. So if the inputs are “ANAGRAM” and “NAAGARM”, then they are anagram, but “cat” and “fat” are not an anagramTo solve this, we will convert the string into a list of characters, then sort them, if two sorted lists are same then they are anagram.Example (Python)Let us see the following implementation to get a better understanding − Live Democlass Solution(object):   ... Read More

Delete Node in a Linked List in Python

sudhir sharma
Updated on 31-Jul-2025 12:49:23

4K+ Views

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. For example: Input: 1 → 3 → 5 → 7 → ... Read More

Invert Binary Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:26:40

978 Views

Suppose we have a binary tree. our task is to create an inverted binary tree. So if the tree is like below −The inverted tree will be likeTo solve this, we will use a recursive approachif the root is null, then returnswap the left and right pointersrecursively solve left subtree and right subtreeExample (Python)Let us see the following implementation to get a better understanding − Live Democlass TreeNode:    def __init__(self, data, left = None, right = None):       self.data = data       self.left = left       self.right = right def make_tree(elements):    Tree = ... Read More

Reverse Linked List in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:57:13

523 Views

Suppose we have a linked list, we have to reverse it. So if the list is like 1 → 3 → 5 → 7, then the new reversed list will be 7 → 5 → 3 → 1To solve this, we will follow this approach −Define one procedure to perform list reversal in a recursive way as to solve(head, back)if the head is not present, then return headtemp := head.nexthead.next := backback = headif temp is empty, then return headhead = tempreturn solve(head, back)ExampleLet us see the following implementation to get a better understanding − Live Democlass ListNode:    def __init__(self, ... Read More

House Robber in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:50:44

1K+ Views

Suppose there is a city, and each house in the city has a certain amount. One robber wants to rob the money in one single night. The city has one security system, that is as if two consecutive houses are broken on the same night, then it will automatically call the police. So we have to find how the maximum amount the robber can rob?One array is provided, at index i, the A[i] is the amount that is present in i-th house. Suppose the array is like: A = [2, 7, 10, 3, 1], then the result will be 13. ... Read More

Advertisements