Tag: List
Converting a Binary Search Tree to a Sorted Doubly Linked List
Change the left and right pointers of each node in Binary search Tree to make it a sorted doubly linked list. Method: Just do the Post order traversal and while visiting the node, Advertisements Point the left link of visiting node to right most node in left tree. Point the right link of …
Converting a Binary Search Tree to a Sorted Doubly Linked ListRead More
Create a Singly Linked List from an array
Suppose we have an int array and we want to create it into a singly linked list from it. Singly Linked List Our Node structure is, Advertisements struct node { int value; node * nextPtr; node(int val) { value = val; nextPtr = NULL; …
Remove Duplicate Elements from a Sorted Singly Linked List
Lets remove all duplicate elements from a sorted singly linked list. For example, Advertisements Suppose given linked list contains following elements in given order, 1, 2, 3, 4, 4, 5, 6, 7, 7, 7, 7, 9, 89 Frequently Asked: Reverse a Singly Linked List in a Single Iteration Create a Singly Linked List from an …
Remove Duplicate Elements from a Sorted Singly Linked ListRead More
Reverse a Singly Linked List in a Single Iteration
Suppose we have a linked list and we want to reverse it in Single iteration and O(n) complexity. Original Linked List, Linked List Reversed Linked List, Advertisements Reversed Linked List Our Node structure is, struct node { int value; node * nextPtr; node(int val) { value = val; …
Different ways to Initialize a list in C++
In this article we will discuss the different ways to initialize a std::list in C++. std::list provides various overloaded constructors for creation and initialization of list. Let’s see different ways to create & initialize a std::list in C++ i.e. Advertisements Creating an Empty List in C++ std::list has a default constructor that will create an empty …
Difference between Vector and List in C++
In this article we will discuss the differences between std::vector and std::list in C++. Both vector and list are sequential containers of C++ Standard Template Library. But there are many differences between them because of their internal implementation i.e. Advertisements List stores elements at non contiguous memory location i.e. it internally uses a doubly linked …
How to remove elements from a List based on value or External Criterion
In this article we will discuss how to remove an element from a List by matching a value or by matching some criterion. std::list provides two member functions for removing elements based on value i.e. std::list::remove and std::list::remove_if. Advertisements Using std::list::remove to remove element by value void remove (const value_type& val); It removes all the elements …
How to remove elements from a List based on value or External CriterionRead More
How to remove elements from a List while Iterating
In this article we will see how to delete an element from a list while iterating through it. Removing Elements From a List while Iterating through it std::list provides a member function erase() that accepts an iterator and deletes the element pointed by that Iterator. But it makes that iterator invalid i.e. we cannot use …
How to erase elements from a list in c++ using iterators
In this article we will see how to erase elements from a std::list using iterators. std::list provides 2 member function to delete elements using iterators i.e. Advertisements iterator erase (const_iterator position); It deletes the element representing by passed iterator “position” and returns the iterator of element next to last deleted element. iterator erase (const_iterator first, const_iterator last); …
How to erase elements from a list in c++ using iteratorsRead More
C++ : How to get element by index in List
In this article we will discuss how to get element by index in std::list. std::list doesn’t have operator [] std::list does not have random access operator [] because std::list internally store elements in doubly linked list. So, to access an element at nth location we need to iterate one by one from beginning to nth element. …