-
Notifications
You must be signed in to change notification settings - Fork 183
/
16 - Day 15 - Linked List.cs
63 lines (57 loc) · 1.14 KB
/
16 - Day 15 - Linked List.cs
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
60
61
62
63
// ========================
// Information
// ========================
// Direct Link: https://www.hackerrank.com/challenges/30-linked-list/problem
// Difficulty: Easy
// Max Score: 30
// Language: C#
// ========================
// Solution
// ========================
using System;
class Node {
public int data;
public Node next;
public Node(int d) {
data = d;
next = null;
}
}
class Solution {
public static Node insert(Node head, int data) {
//Complete this method
Node node = new Node(data);
if (head == null) {
head = node;
}
else {
if (head.next == null) {
head.next = node;
}
else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = node;
}
}
return head;
}
public static void display(Node head) {
Node start = head;
while (start != null) {
Console.Write(start.data + " ");
start = start.next;
}
}
static void Main(String[] args) {
Node head = null;
int T = Int32.Parse(Console.ReadLine());
while (T-->0) {
int data = Int32.Parse(Console.ReadLine());
head = insert(head, data);
}
display(head);
}
}