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
Programming Articles
Page 400 of 2547
Python Program to Select the nth Smallest Element from a List in Expected Linear Time
Finding the nth smallest element from a list in expected linear time requires implementing the Quickselect algorithm. This algorithm uses partitioning similar to quicksort but only searches one partition, achieving O(n) average time complexity. The algorithm works by selecting a pivot element, partitioning the list around it, and then recursively searching only the partition that contains the nth smallest element. Algorithm Implementation Here's the implementation using two functions − one for selection and another for partitioning ? def select_smallest(numbers, beg, end, i): if end - beg k: ...
Read MorePython Program to Check String is Palindrome using Stack
When it is required to check if a string is a palindrome using stack data structure, a stack class is created, and push and pop methods are defined to add and delete values from stack. Another method checks to see if the stack is empty or not. A palindrome is a word that reads the same forwards and backwards, like "madam" or "racecar". Using a stack's LIFO (Last In, First Out) property, we can reverse a string and compare it with the original. Stack Implementation First, let's create a stack class with basic operations ? ...
Read MorePython Program to Reverse only First N Elements of a Linked List
When it is required to reverse a specific set of elements in a linked list, a method named 'reverse_list' is defined. This iterates through the list and reverses the first N elements while keeping the remaining elements unchanged. Below is a demonstration for the same − Example class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_structure: def __init__(self): self.head ...
Read MorePython Program to Find Number of Occurrences of All Elements in a Linked List
When working with linked lists, it's often useful to find the number of occurrences of elements. This implementation demonstrates how to create a linked list and count occurrences of specific elements using a custom count_elem method. Below is a demonstration for the same − Linked List Implementation First, we create the basic structure with Node and LinkedList classes: class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_structure: ...
Read MorePython Program to Find the first Common Element between the 2 given Linked Lists
When it is required to find the common element that occurs for the first time between two linked lists, a method to add elements to the linked list, and a method to get the common element that occurs for the first time in these linked lists is defined. Below is a demonstration for the same − Example class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_structure: def ...
Read MorePython Program to Add Corresponding Positioned Elements of 2 Linked Lists
When working with linked lists, you may need to add corresponding elements from two lists at the same positions. This involves traversing both lists simultaneously and creating a new linked list with the sum of elements at each position. Below is a demonstration for the same − Node and LinkedList Classes First, we define the basic structure for nodes and linked lists ? class Node: def __init__(self, data): self.data = data self.next = None ...
Read MorePython Program to Implement Queue Data Structure using Linked List
A queue is a linear data structure that follows the First In First Out (FIFO) principle. When implementing a queue using a linked list, we need methods to add elements at the rear (enqueue) and remove elements from the front (dequeue). Queue Implementation Structure Our queue implementation consists of two classes ? class Node: def __init__(self, data): self.data = data self.next = None class Queue: def __init__(self): ...
Read MorePython Program to Implement a Stack using Linked List
When implementing a stack data structure using a linked list, we need methods to add (push) elements to the top and remove (pop) elements from the top. In a stack, the last element added is the first one to be removed (LIFO - Last In First Out). Node Class First, we create a Node class to represent individual elements in the linked list ? class Node: def __init__(self, data): self.data = data self.next = None ...
Read MorePython Program to Print the Alternate Nodes in a Linked List using Recursion
When working with linked lists, we often need to print alternate nodes (1st, 3rd, 5th, etc.). This can be achieved using recursion by skipping one node at each recursive call. We'll create a linked list class with methods to add elements and a recursive function to print alternate nodes. Node Class First, let's define a simple Node class to represent individual elements in our linked list ? class Node: def __init__(self, data): self.data = data self.next ...
Read MorePython Program to Find the Length of the Linked List without using Recursion
When it is required to find the length of a linked list without using recursion, we define methods to add elements to the linked list and calculate its length using iterative approach. This approach uses a simple loop to traverse through all nodes and count them. Below is a demonstration for the same − Example class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): ...
Read More