Repository navigation
Expand file tree
/
Copy pathLinklist.java
More file actions
148 lines (131 loc) · 3.29 KB
/
Copy pathLinklist.java
File metadata and controls
148 lines (131 loc) · 3.29 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
public class Linklist {
node head;
private int size;
Linklist(){
this.size=0;
}
class node{
String data;
node next;
node(String data){
this.data=data;
this.next=null;
size++;
}
}
// add-first,last
public void addfirst(String data){
node newnode= new node(data);
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;
}
node currNode=head;
while(currNode.next != null){
currNode=currNode.next;
}
currNode.next=newnode;
}
public void printList(){
if (head == null) {
System.out.println("list is empty");
return;
}
node currNode=head;
while(currNode != null){
System.out.print(currNode.data+" --> ");
currNode=currNode.next;
}
System.out.println("NULL");
}
// delete first
public void deletefirst(){
if(head==null){
System.out.println("The list is empty");
return;
}
size--;
head=head.next;
}
// delete last
public void deletelast(){
if(head==null){
System.out.println("The list is empty");
return;
}
size--;
if (head.next == null) {
head=null;
return;
}
node secondlast = head;
node lastnode=head.next;
while(lastnode.next!=null){
lastnode=lastnode.next;
secondlast = secondlast.next;
}
secondlast.next=null;
}
public int getsize(){
return 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;
// update
prevNode=currNode;
currNode=nextNode;
}
head.next=null;
head=prevNode;
}
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 list =new Linklist();
// list.addfirst("a");
// list.addfirst("is");
// list.printList();
// list.addlast("list");
// list.printList();
// list.addfirst("this");
// list.printList();
// list.deletefirst();
// list.printList();
// list.deletelast();
// list.printList();
// System.out.println(list.getsize());
// list.addfirst("this");
// list.printList();
// System.out.println(list.getsize());
list.addlast("a");
list.addlast("b");
list.addlast("c");
list.addlast("d");
list.printList();
list.head=list.reverserecursive(list.head);
list.printList();
}
}