-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathLinkedIistImplementation.java
More file actions
97 lines (83 loc) · 1.98 KB
/
LinkedIistImplementation.java
File metadata and controls
97 lines (83 loc) · 1.98 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
public class LinkedList {
Node head;
Node tail;
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
}
}
// Adding node into the first postion of linkde list
// t.n 0(1)
public void addFirst(int data) {
Node node = new Node(data);
node.next = head;
head = node;
}
// Display all Node Value
public void DisplayValue() {
Node currNode = head;
while (currNode != null) {
System.out.print(currNode.data + "=>");
currNode = currNode.next;
}
System.out.println();
}
// t.n 0(n)
public void addLast(int data) {
Node node = new Node(data);
// if the linked list is null
if (head == null) {
head = node;
}
Node currNode = head;
while (currNode.next != null) {
currNode = currNode.next;
}
currNode.next = node;
}
// t.n 0(1)
public void removeFirst() {
if (head == null) {
return;
}
head = head.next;
}
// Remove Last Element form the linked list
// t.c 0(n)
public void removeLast() {
// Home Work
if (head == null) {
return;
}
if (head.next == null) {
head = null;
}
Node prev = head;
Node nextNode = head.next;
while (nextNode.next != null) {
prev = prev.next;
nextNode = nextNode.next;
}
prev.next = null;
}
public static void main(String[] args) {
LinkedList li = new LinkedList();
li.addFirst(34);
li.addFirst(45);
li.addFirst(50);
li.addFirst(100);
li.addLast(60);
li.DisplayValue();
li.removeLast();
li.DisplayValue();
}
}
// Linked list last element
// Reverse Linked List
// 1-2-3-4 linked list
// tail
// DoublyLinked List
//