-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkList_reverse.java
More file actions
168 lines (136 loc) · 4.7 KB
/
LinkList_reverse.java
File metadata and controls
168 lines (136 loc) · 4.7 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.util.LinkedList;
import java.util.jar.Attributes.Name;
//ek LL di hai use sirf reverse karna hai
// [1 , 2 , 3 , 4 ] ...
// [4 , 3 , 2 , 1 ]..
//by iterate
//isme ham 3 verable banayenge (prev , curr , next);;
//curr ka next ko hame prev banana hai;;--> curr.next=prev;
//
public class LinkList_reverse {
node head;
private int size; // private size ka int banaya
LinkList_reverse() {
this.size = 0; // size ko initialize 0 se kr diya ;;;
}
class node { // node cclass bana li hamne
int data;
node next;
node(int data) { // CONSTRUCTOR bana liya
this.data = data;
this.next = null;
size++;
}
}
// ADD--1.FIRST...2.LAST
public void addfirst(int data) {
node newnode = new node(data);
if (head == null) {
head = newnode;
return;
}
newnode.next = head;
head = newnode;
}
// ADD LAST
public void addlast(int data) {
node newnode = new node(data);
if (head == null) {
head = newnode;
return;
}
node currnode = head;
while (currnode.next != null) {
currnode = currnode.next;
}
currnode.next = newnode;
}
// print
public void printlist() {
if (head == null) {
System.out.println(" LL is empty");
return;
}
node currnode = head;
while (currnode != null) {
System.out.print(currnode.data + "-->");
currnode = currnode.next;
}
System.out.println("null");
}
// DELETE
// first ko delete karna hai to first ke next ko head banana hoga
// last ko agr delete karna hai ..to next-next karna hai aur jaise hi second
// last node tak pohochenge .
// to second last node ko null bana lenge
// delete first
public void deletefirst() {
if (head == null) {
System.out.println("LL is empty");
return;
}
size--; // delete hone ke bad size ko kam karna padega
head = head.next; // head.next ko head banaya to head carbej ho gya
}
public void deletelast() {
if (head == null) {
System.out.println("LL is empty");
return;
}
size--; //// delete hone ke bad size ko kam karna padega
node seclast = head; // head ko sechead banaya
node lastnode = head.next; // head next ko last node banaaya
if (head.next == null) { // jisme LL me ek hi node hoga //next karte wakt null ka next null nahi ho sakta
head = null;
return;
}
while (lastnode.next != null) { // jabtak last node null nahi ho jata
lastnode = lastnode.next; // lastnode next ko last node me dalneka
seclast = seclast.next; // aur seclast node ke next ko sec last me dalneka jo ki head banaya hai
}
seclast.next = null;
// lastnode = null;
}
public int getsize() {
return size;
}
public void reverseiterate() {
if (head == null || head.next == null) {
return;
}
node prenode = head; // head ko prenode kr diya
node currnode = head.next; // head ke next ko currnode kr diya
while (currnode != null) { // jab tk currnode null nahi ho jyata
node nextnode = currnode.next; // currnode ke next ko nextnode kr diya;;
currnode.next = prenode; // prenode ko currnode ke next me dalne ka ;;;
// uodate
prenode = currnode; // simply aage ka ek kadam pichhe aayega ;;; currrnode ko prenode matlan ek
// kadam pichhe
currnode = nextnode; // nextnode ko currnode
}
head.next = null; // last me head ka next null hoga
head = prenode; // head hoga prenode;;;
}
public node reverserecursive(node head) {
if (head == null || head.next == null) {
return head;
}
node newhead = reverserecursive(head.next);
head.next.next = head;
head.next = null;
return newhead;
}
public static void main(String[] args) {
LinkList_reverse list = new LinkList_reverse();
list.addfirst(1);
list.addfirst(2);
list.addfirst(3);
list.addfirst(4);
list.printlist();
// list.reverseiterate();
// list.printlist();
list.head = list.reverserecursive(list.head);
list.printlist();
}
}
// ye hame iteratively reverse kiya