[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.
C++
#include<iostream>usingnamespacestd;// A linked list nodeclassNode{public:intdata;Node*next;// Constructor to initialize a new node with dataNode(intnew_data){this->data=new_data;this->next=nullptr;}};// Function to print the singly linked listvoidprintList(Node*head){// A loop that runs till head is nullptrwhile(head!=nullptr){// Printing data of current nodecout<<head->data;if(head->next)cout<<"->";// Moving to the next nodehead=head->next;}}intmain(){// Create a linked list: 10 -> 20 -> 30 -> 40Node*head=newNode(10);head->next=newNode(20);head->next->next=newNode(30);head->next->next->next=newNode(40);printList(head);return0;}
C
#include<stdio.h>#include<stdlib.h>// A linked list nodestructNode{intdata;structNode*next;};// Function to create a new nodestructNode*createNode(intnew_data){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=new_data;node->next=NULL;returnnode;}// Function to print the singly linked listvoidprintList(structNode*head){// A loop that runs till head is NULLwhile(head!=NULL){// Printing data of current nodeprintf("%d",head->data);if(head->next)printf("->");// Moving to the next nodehead=head->next;}}intmain(){// Create a linked list: 10 -> 20 -> 30 -> 40structNode*head=createNode(10);head->next=createNode(20);head->next->next=createNode(30);head->next->next->next=createNode(40);printList(head);return0;}
Java
classNode{intdata;Nodenext;// Constructor to initialize a new node with dataNode(intnewData){this.data=newData;this.next=null;}}classGfG{// Function to print the singly linked liststaticvoidprintList(Nodehead){// A loop that runs till head is nullwhile(head!=null){// Printing data of current nodeSystem.out.print(head.data);if(head.next!=null)System.out.print("->");// Moving to the next nodehead=head.next;}}publicstaticvoidmain(String[]args){// Create a linked list: 10 -> 20 -> 30 -> 40Nodehead=newNode(10);head.next=newNode(20);head.next.next=newNode(30);head.next.next.next=newNode(40);printList(head);}}
Python
# A linked list nodeclassNode:def__init__(self,newData):# Constructor to initialize a new node with dataself.data=newDataself.next=None# Function to print the singly linked listdefprintList(node):whilenodeisnotNone:print(f"{node.data}",end="")ifnode.nextisnotNone:print("->",end="")node=node.nextprint()if__name__=="__main__":# Create a linked list: 10 -> 20 -> 30 -> 40head=Node(10)head.next=Node(20)head.next.next=Node(30)head.next.next.next=Node(40)printList(head)
C#
usingSystem;classNode{publicintdata;publicNodenext;// Constructor to initialize a new node with datapublicNode(intnew_data){this.data=new_data;this.next=null;}}classGfG{// Function to print the singly linked liststaticvoidprintList(Nodehead){// A loop that runs till head is nullwhile(head!=null){// Printing data of current nodeConsole.Write(head.data+"");if(head.next!=null)Console.Write("->");// Moving to the next nodehead=head.next;}}staticvoidMain(string[]args){// Create a linked list: 10 -> 20 -> 30 -> 40Nodehead=newNode(10);head.next=newNode(20);head.next.next=newNode(30);head.next.next.next=newNode(40);printList(head);}}
JavaScript
classNode{constructor(newData){// Constructor to initialize a new node with datathis.data=newData;this.next=null;}}// Function to print the singly linked listfunctionprintList(node){while(node!==null){process.stdout.write(node.data.toString());if(node.next!==null){process.stdout.write("->");}node=node.next;}process.stdout.write("\n");}// Driver Code// Create a linked list: 10 -> 20 -> 30 -> 40consthead=newNode(10);head.next=newNode(20);head.next.next=newNode(30);head.next.next.next=newNode(40);printList(head);
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.
C++
#include<iostream>usingnamespacestd;// A linked list nodeclassNode{public:intdata;Node*next;// Constructor to initialize a new node with dataNode(intnew_data){this->data=new_data;this->next=nullptr;}};// Function to print the singly linked listvoidprintList(Node*head){// Base condition is when the head is nullptrif(head==nullptr){return;}// Printing the current node datacout<<head->data;if(head->next)cout<<"->";// Moving to the next nodeprintList(head->next);}intmain(){// Create a linked list: 10 -> 20 -> 30 -> 40Node*head=newNode(10);head->next=newNode(20);head->next->next=newNode(30);head->next->next->next=newNode(40);printList(head);return0;}
C
#include<stdio.h>#include<stdlib.h>// A linked list nodestructNode{intdata;structNode*next;};// Function to create a new node with given datastructNode*createNode(intnew_data){structNode*new_node=(structNode*)malloc(sizeof(structNode));new_node->data=new_data;new_node->next=NULL;returnnew_node;}// Function to print the singly linked listvoidprintList(structNode*head){// Base condition is when the head is nullptrif(head==NULL){return;}// Printing the current node dataprintf("%d",head->data);if(head->next)printf("->");// Moving to the next nodeprintList(head->next);}intmain(){// Create a linked list: 10 -> 20 -> 30 -> 40structNode*head=createNode(10);head->next=createNode(20);head->next->next=createNode(30);head->next->next->next=createNode(40);printList(head);return0;}
Java
// A linked list nodeclassNode{intdata;Nodenext;// Constructor to initialize a new node with dataNode(intnew_data){data=new_data;next=null;}}classGfG{// Function to print the singly linked liststaticvoidprintList(Nodehead){// Base condition is when the head is nullptrif(head==null){return;}// Printing the current node dataSystem.out.print(head.data);if(head.next!=null)System.out.print("->");// Moving to the next nodeprintList(head.next);}publicstaticvoidmain(String[]args){// Create a linked list: 10 -> 20 -> 30 -> 40Nodehead=newNode(10);head.next=newNode(20);head.next.next=newNode(30);head.next.next.next=newNode(40);printList(head);}}
Python
# A linked list nodeclassNode:def__init__(self,data):# Constructor to initialize a new node with dataself.data=dataself.next=None# Function to print the singly linked listdefprintList(node):whilenodeisnotNone:print(f"{node.data}",end="")ifnode.nextisnotNone:print("->",end="")node=node.nextprint()if__name__=="__main__":# Create a linked list: 10 -> 20 -> 30 -> 40head=Node(10)head.next=Node(20)head.next.next=Node(30)head.next.next.next=Node(40)printList(head)
C#
usingSystem;// A linked list nodeclassNode{publicintData{get;set;}publicNodeNext{get;set;}// Constructor to initialize a new node with datapublicNode(intnewData){Data=newData;Next=null;}}classGfG{// Function to print the singly linked liststaticvoidprintList(Nodehead){// Base condition is when the head is nullptrif(head==null){return;}// Printing the current node dataConsole.Write(head.Data+"");if(head.Data!=null)Console.Write("->");// Moving to the next nodeprintList(head.Next);}staticvoidMain(){// Create a linked list: 10 -> 20 -> 30 -> 40Nodehead=newNode(10);head.Next=newNode(20);head.Next.Next=newNode(30);head.Next.Next.Next=newNode(40);printList(head);}}
JavaScript
// A linked list nodeclassNode{constructor(new_data){// Constructor to initialize a new node with datathis.data=new_data;this.next=null;}}// Function to print the singly linked listfunctionprintList(head){// Base condition is when the head is nullif(head===null){return;}// Printing the current node dataprocess.stdout.write(head.data.toString());if(head.next!==null){process.stdout.write("->");}// Moving to the next nodeprintList(head.next);}// Create a linked list: 10 -> 20 -> 30 -> 40lethead=newNode(10);head.next=newNode(20);head.next.next=newNode(30);head.next.next.next=newNode(40);printList(head);