# Displaying the Nodes

### Iterative Method :

```c
void display(struct node *ptr) {
    
    // Checking if list is empty
    if (ptr == NULL) {
        printf("The list is empty\n");
    }
    // Iterate till the end otherwise
    else {
        while (ptr != NULL) {
            printf("%d\t", ptr -> data);
            ptr = ptr -> next;
        }
    }
    
}
```

### Recursive Method :

```c
void display(struct node *ptr) {

    if (ptr != NULL) {
        printf("%d\t", ptr -> data);
        display(ptr -> next);
    }

}
```

### Recursive & Reversed :

```c
void display(struct node *ptr) {

    if (ptr != NULL) {
        display(ptr -> next);
        printf("%d\t", ptr -> data);
    }

}
```

### Time and Space Complexity :

{% tabs %}
{% tab title="Iterative" %}
Time Complexity : **O(n)**\
**No extra space**
{% endtab %}

{% tab title="Recursive" %}
Time complexity : **O(n)**\
**Internal stack of size n+1 is used.**
{% endtab %}
{% endtabs %}

Contributed by Nitin Ranganath


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nitinranganath.gitbook.io/data-structures/linked-list/displaying-the-nodes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
