-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglylinkedlists.java
173 lines (156 loc) · 4.3 KB
/
Singlylinkedlists.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
161
162
163
164
165
166
167
168
169
170
171
172
173
public class Singlylinkedlists{
private ListNode head;
private static class ListNode{
private int data;
private ListNode next;
public ListNode(int data){
this.data =data;
this.next =null;
}
}
public void display(ListNode head){
ListNode current =head;
while (current !=null){
System.out.print(current.data+ "-->");
current =current.next;
}
System.out.print("null");
System.out.println("\n");
}
public int length(){
if(head ==null){
return 0;
}
int count =0;
ListNode current =head;
while(current!=null){
count++;
current= current.next;
}
return count;
}
public ListNode insertfirst(int value){
ListNode newNode = new ListNode(value);
newNode.next =head;
head = newNode;
return newNode;
}
public ListNode insertlast(int value){
ListNode newNode =new ListNode(value);
if(head ==null){
head =newNode;
return newNode;
}
ListNode current =head;
while (current.next!=null){
current =current.next;
}
current.next =newNode;
return newNode;
}
public ListNode insert(int position, int value){
ListNode newNode =new ListNode(value);
if(position == 1){
newNode.next =head;
head =newNode;
return newNode;
}
else{
ListNode previous =head;
int count=1;
while (count <position-1) {
previous =previous.next;
count++;
}
ListNode current =previous.next;
newNode.next =current;
previous.next =newNode;
return newNode;
}
}
public ListNode removefirst(){
ListNode current = head;
head =head.next;
current.next =null;
return current;
}
public ListNode removelast(){
if(head == null){
return null;
}
ListNode current = head;
ListNode previous =null;
while (current.next !=null){
previous =current;
current =current.next;
}
previous.next =null;
return current;
}
public ListNode remove(int position){
if (head==null || position ==1){
return null;
}
ListNode current = head;
int count =1;
while (count < position-1){
current =current.next;
count++;
}
ListNode previous = current.next;
current.next =previous.next;
return current;
}
public boolean search(int searchkey){
ListNode current =head;
while(current !=null){
if(current.data == searchkey){
return true;
}
current = current.next;
}
return false;
}
public ListNode reverse(ListNode head){
if(head ==null){
return head;
}
ListNode current =head;
ListNode previous =null;
ListNode next =null;
while(current!= null){
next = current.next;
current.next =previous;
previous = current;
current =next;
}
return previous;
}
public static void main(String[] args){
Singlylinkedlists s = new Singlylinkedlists();
s.head = new ListNode(10);
ListNode second = new ListNode(1);
ListNode third = new ListNode(8);
ListNode fourth = new ListNode(11);
s.head.next = second;
second.next = third;
third.next = fourth;
//s.insertfirst(2);
//s.insertlast(3);
//s.insert(3, 5);
//s.removefirst();
//s.removelast();
//s.remove(3);
//boolean isFound =s.search(11);
// if(isFound){
// System.out.println("Found the element");
// }
// else{
// System.out.println("not found");
// }
s.display(s.head);
ListNode reverseList =s.reverse(s.head);
s.display(reverseList);
//System.out.println("the length is " + s.length());
}
}