VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-linked-list/

⇱ Print Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print Linked List

Last Updated : 6 Aug, 2025

Given a head of Singly Linked List, we have to print all the elements in the list.

Examples:

Input:

👁 linkedlist1

Output: 1->2->3->4->5
Input:

👁 linkedlist2

Output: 10->20->30->40->50

[Approach 1] Using Iterative Method - O(n) Time and O(1) Space

Start from the head and follow the next pointer to visit each node, printing the data until the next pointer is NULL.
Steps to solve the problem:

  • Start from the head node.
  • While the current node is not NULL:
  • Print the node's data.
  • Move to the next node using the next pointer

Program to Print the Singly Linked List using Iteration.


Output
10->20->30->40

[Approach 2] Using Recursion Method- O(n) Time and O(n) Space

At each node, print the data and recursively call the function using the next pointer until the node becomes NULL.

Program to Print the Singly Linked List using Recursion.


Output
10->20->30->40
Comment
Article Tags: