linked list

Can anyone help to explain how is the class Solution compile?
I tried to understand as below:
First judge if head is null,for the first time call this function,head is null,so build a new node,then second time call,now the head is not null,so set a node whose name is curr,point to where head point,then where is curr->next point to?Anyway,it is not null,right?So we set curr point to where curr->next point to,then build a new node?I am confused in here.Thanks,I want to draw a figure to show this process,but I don't know where should I put this data:curr,curr->next..Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  #include <iostream>
#include <cstddef>
using namespace std;	
class Node
{
    public:
        int data;
        Node *next;
        Node(int d){
            data=d;
            next=NULL;
        }
};

class Solution{
    public:      
   Node* insert(Node *head,int data)
      {
        if (head == NULL)
        {
            head = new Node(data);
        }
        else
        {
            Node *curr = head;
            while (curr->next != NULL)
                curr = curr->next;
            
            curr->next = new Node(data);
        }
        
        return head;
      }

 void display(Node *head)
      {
          Node *start=head;
          while(start)
          {
              cout<<start->data<<" ";
              start=start->next;
          }
      }
};


int main()
{
	Node* head=NULL;
  	Solution mylist;
    int T,data;
    cin>>T;
    while(T-->0){
        cin>>data;
        head=mylist.insert(head,data);
    }	
	mylist.display(head);
		
}
Firstly :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
      int T, data;

      cout << "Input the number of nodes you need to create : ";
      cin >> T; cout << endl;

      for(int i = 0; i < T; ++i)
      {
            cout << "Input the value of Node(" << i+1 << ") : ";
            cin >> data;
            head =mylist.insert(head, data);
      }

      cout << endl;
      cout << "Printing the values of all nodes : " << endl;      
      mylist.display(head);


1
2
3
4
5
6
7
8
9
void display(Node *head)
{
      Node *start=head;
      while(start)
      {
            cout << "/t" << start->data << endl;
            start = start->next;
      }
}


Please make your program more informative and interactive enough (if you can)
Sorry,the information are below.
Insert function should return a reference to the node of the linked list.

Sample Input
The first line contains T, the number of test cases.
The subsequent lines of test cases each contain an integer to be inserted at the list's tail.
4
2
3
4
1

Sample Output
Prints the ordered data values for each element in your list as a single line of space-separated integers:

2 3 4 1
Topic archived. No new replies allowed.