forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cpp
39 lines (35 loc) · 916 Bytes
/
Solution.cpp
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
/*
Problem: https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list
Language : C++
Tool Version : Visual Studio Community 2017
Thoughts :
1. Let the head node pointer be h.
2. Start a loop
2.1 If h is null then quit the loop.
2.2 If h is not null then print the data portion of node pointed by h.
2.3 Move h to node pointed by next pointer of node currently pointed by h.
2.4 Go to step 2.1
3. End
Time Complexity: O(n)
Space Complexity: O(1) //number of dynamically allocated variables remain constant for any length of linked list.
*/
/*
Print elements of a linked list on console
head pointer input could be NULL as well for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
void Print(Node *head)
{
if (head == NULL)
return;
do
{
printf("%d\n", head->data);
head = head->next;
} while (head != NULL);
}