How to Print Linked List in C++? Last Updated : 23 May, 2024 Comments Improve Suggest changes Like Article Like Report A Linked List is a linear data structure that consists of nodes having a data member and a pointer to the next node. The linked list allows the users to store data of the same type in non-contiguous memory locations through dynamic memory allocation. In this article, we will learn how to print a linked list in C++. Example Input: Linked List: 1->2->3->4->5 Output: 1 2 3 4 5Print Linked List in C++To print a linked list in C++, we can follow the below algorithm: Algorithm to Print a Linked ListStartInitialize a pointer temp to point at the head of the linked list.While temp is not equal to nullptr:Print temp->data.Move temp to the next node using temp=temp->next.StopC++ Program to Print a Linked ListThe following program illustrates how we can print a linked list in C++: C++ // C++ Program to demonstratre how to Print a Linked List #include <iostream> using namespace std; // Node definition class Node { public: int data; Node* next; Node(int val) { data = val; next = nullptr; } }; // Linked List definition class LinkedList { private: Node* head; public: LinkedList() { head = nullptr; } // Function to add a node at the end of the list void insert(int val) { Node* newNode = new Node(val); if (head == nullptr) { head = newNode; return; } Node* temp = head; while (temp->next != nullptr) { temp = temp->next; } temp->next = newNode; } // Function to print the linked list void printList() { Node* temp = head; while (temp != nullptr) { cout << temp->data << " "; temp = temp->next; } cout << endl; } }; int main() { // Declare an object of the linked list class LinkedList list; // insert some values to the list list.insert(1); list.insert(2); list.insert(3); list.insert(4); list.insert(5); // Print the linked list list.printList(); return 0; } Output1 2 3 4 5 Time Complexity: O(N), where N is the number of nodes in the linked list.Auxiliary Space: O(1), as no extra space is used to print the linked list. Comment More infoAdvertise with us Next Article How to Print Linked List in C++? S satwiksuman Follow Improve Article Tags : C++ Programs C++ CPP Examples Practice Tags : CPP Similar Reads Linked List C/C++ Programs The Linked Lists are linear data structures where the data is not stored at contiguous memory locations so we can only access the elements of the linked list in a sequential manner. Linked Lists are used to overcome the shortcoming of arrays in operations such as deletion, insertion, etc. In this ar 2 min read How to Sort a List in C++ STL? In C++, a list is a sequence container provided by the STL library of C++ that provides the features of a doubly linked list and stores the data in non-contiguous memory locations efficiently. In this article, we will learn how to sort a list in C++. Example: Input: myList = {30, 10, 20, 40, 50};Out 2 min read Array of Linked Lists in C/C++ An array in C/C++ or be it in any programming language is a collection of similar data items stored at contiguous memory locations and elements that can be accessed randomly using indices of an array. Â They can be used to store the collection of primitive data types such as int, float, double, char, 3 min read Correct the Random Pointer in Doubly Linked List Given a doubly linked list having exactly one of the node pointing to a random node in the list, the task is to correct this random pointer in the doubly linked list, such that it points to the expected node.Examples: Input: Output: Explanation: 2's next pointer has been corrected to point to 3. Ear 8 min read How to Copy a List in C++ STL? In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows us to store data in non-contiguous memory locations efficiently. In this article, we will learn how to copy one list to another in C++. Input: sourceList = {10, 20, 30, 40, 50};Output: 2 min read How to Reverse a List in C++ STL? In C++, std::list is a sequence container that allows non-contiguous memory allocation. As such, it is a doubly linked list that can be traversed in both directions. In this article, we will learn how to reverse a list in C++. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Reversed List: 50 2 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read Program to Implement Singly Linked List in C++ Using Class A singly linked list is a linear data structure where each element (node) points to the next element in the sequence. It consists of nodes, with each node having two components: a data part to store the value and a next pointer part to store the address of the next node.Traditionally, we represent t 3 min read How to Find the Size of a List in C++? In C++, Standard Template Library (STL) we have a std::list container that is a doubly-linked list in which elements are stored in non-contiguous memory allocation. In this article, we will learn how to find the size of a list in C++ STL. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Size o 2 min read C++ Program for Deleting a Node in a Linked List Write a C++ program to delete a node from the given link list.ExamplesInput: Linked List: 10 -> 20 -> 30 -> 40 -> 50, Position to delete: 3Output: 10 -> 20 -> 40 -> 50Explanation: The node at position 3 is removed. The list then connects node 20 directly to node 40.Input: Linked 6 min read Like