-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList.java
160 lines (129 loc) · 3.89 KB
/
linkedList.java
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
155
156
157
158
159
160
import java.util.LinkedList;
public class linkedList {
Node head;
private int size = 0;
// this class is created to make the node of our linked list
class Node{
String data;
Node next;
Node(String data){
this.data = data;
this.next = null;
}
}
/*
* Adding element is linked list
* add first , add last
*/
public void addFirst(String data){
Node newnode = new Node(data);
size++;
if (head == null) {
head = newnode;
return;
}
newnode.next = head;
head = newnode;
}
public void addLast(String data){
Node newnode = new Node(data);
if (head == null) {
head = newnode;
return;
}
size++;
Node currentNode = head;
while (currentNode.next != null) {
currentNode = currentNode.next;
}
currentNode.next = newnode;
}
public void printList(){
Node currentNode = head;
while (currentNode != null) {
System.out.print(currentNode.data + "-->");
currentNode = currentNode.next;
}
System.out.println("NULL");
}
public static void main(String[] args) {
// Linked List
/*
* It store data in simple form of nodes. Nodes consist 2 data , 1st is information and 2nd is next.
*
* Information is data of that particular node and Next is pointer that points to next element of linked list.
*
* First node is called head of link list.
*
* Types of linked list :-
* 1.Singular
* 2.Double
* 3.Circular
*
* In double type:- previous and next both are stored
* Circular :- last node connects to first node. so at end null node is not there
*
* when we have to do insertion lots of time we use linked list as its time complexity is O(1);
* when we have to do searching lots of time we use arraylist as its time complexity is O(1) while linked list's time complexity is O(n);
*
*/
// Creating linkedList from scratch.
linkedList list_from_scratch = new linkedList();
list_from_scratch.addFirst("a");
list_from_scratch.addFirst("is");
list_from_scratch.addLast("jenish");
list_from_scratch.printList();
}
// delete from linked list :- delete first , delete last;
public void deleteFirst(String data){
if (head == null) {
System.out.println("list is empty");
return;
}
size--;
head = head.next;
}
public void deleteLast(String data){
if (head == null) {
System.out.println("list is empty");
return;
}
size--;
if (head.next == null) {
head = null;
return;
}
Node secondlastNode = head;
Node lastNode = head.next;
while (lastNode.next !=null) {
lastNode = lastNode.next;
secondlastNode = secondlastNode.next;
}
secondlastNode.next = null;
}
public void getSize(){
System.out.println(size);
}
public void reverseIterate(){
if (head == null || head.next == null) {
return;
}
Node prevNode = head;
Node currNode = head.next;
while(currNode != null){
Node nextNode = currNode.next;
currNode.next = prevNode;
prevNode = currNode;
currNode = nextNode;
}
}
public Node reverRecursive(Node node){
if (head == null || head.next == null) {
return head;
}
Node newHead = reverRecursive(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
}