-
Notifications
You must be signed in to change notification settings - Fork 690
/
LinkedList.dart
79 lines (67 loc) · 1.24 KB
/
LinkedList.dart
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
// LinkedList Node
class Node{
int val;
Node next;
Node(int x){
this.val = x;
this.next = null;
}
}
class LinkedList{
Node start;
int length;
LinkedList([Node n=null]){
if (n != null) {
this.start = n;
this.length = 1;
}
else{
this.start = null;
this.length = 0;
}
}
void insert_end(Node n){
if (this.start == null){
this.start = n;
this.length += 1;
return;
}
Node temp = this.start;
while (temp.next != null){
temp = temp.next;
}
temp.next = n;
this.length += 1;
}
void delete_end(){
if (this.start == null){
return;
}
Node temp = this.start;
while (temp.next.next != null){
temp = temp.next;
}
temp.next = null;
this.length -= 1;
}
void display(){
Node temp = this.start;
while (temp != null){
print(temp.val);
temp = temp.next;
}
}
}
main(){
LinkedList l = LinkedList();
l.insert_end(Node(3));
l.insert_end(Node(5));
l.insert_end(Node(2));
l.insert_end(Node(1));
l.insert_end(Node(7));
l.display();
l.delete_end();
l.delete_end();
l.display();
}
// This code was contributed by Surya Kant Sahu (https://ojus1.github.io)