-
Notifications
You must be signed in to change notification settings - Fork 183
/
25 - Day 24 - More Linked Lists.cs
79 lines (67 loc) · 1.41 KB
/
25 - Day 24 - More Linked Lists.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// ========================
// Information
// ========================
// Direct Link: https://www.hackerrank.com/challenges/30-linked-list-deletion/problem
// Difficulty: Easy
// Max Score: 30
// Language: C#
// ========================
// Solution
// ========================
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node next;
public Node(int d) {
data = d;
next = null;
}
}
class Solution {
public static Node removeDuplicates(Node head) {
//Write your code here
Node start = head;
while (start.next != null) {
if (start.data == start.next.data) {
start.next = start.next.next;
} else {
start = start.next;
}
}
return head;
}
public static Node insert(Node head, int data) {
Node p = new Node(data);
if (head == null) {
head = p;
}
else if (head.next == null) {
head.next = p;
}
else {
Node start = head;
while (start.next != null)
start = start.next;
start.next = p;
}
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);
}
head = removeDuplicates(head);
display(head);
}
}