-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathLinkedList.java
More file actions
154 lines (124 loc) · 3.09 KB
/
LinkedList.java
File metadata and controls
154 lines (124 loc) · 3.09 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
public class LinkedList {
static Node head;
static Node tail;
private int size = 0;
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);
if(head==null) tail = node;
node.next = head;
head = node;
size++;
}
// 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)
// t.c should be 0(1)
public void InsertLast () {
}
// 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;
// size++;
// }
// t.n 0(1)
public void removeFirst() {
if (head == null) {
return;
}
head = head.next;
size--;
}
// 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;
size--;
}
public int getSize() {
return size;
}
public static Node getMiddleElement() {
Node prev;
Node fast;
prev = head;
fast = head;
while (fast != null && fast.next != null) {
prev = prev.next;
fast = fast.next.next;
}
return prev;
}
public static void main(String[] args) {
LinkedList li = new LinkedList();
li.addFirst(34);
li.addFirst(45);
li.addFirst(50);
li.addFirst(100);
li.addLast(67);
li.addLast(64);
li.addLast(34);
li.addLast(62);
li.DisplayValue();
li.removeLast();
li.DisplayValue();
// System.out.print(li.getSize());
Node middleElement = getMiddleElement();
System.out.print(middleElement.data);
}
}
// Linked list last element
// Reverse Linked List
// 1-2-3-4 linked list
// tail
// DoublyLinked List
//
// Getting the size of linked List
// Insert element after given node
// Reverse a linked list
// Insert in the last 0(1) tail pointer
//
// Add Element in last of linked list 0(1)
// Remove element form last in linked list 0(1)
// Reverse A linked list
// Doubly Linked list
// Circular list
// Detect a loop in linked list
//