-
Notifications
You must be signed in to change notification settings - Fork 17
/
DeleteAlternate.java
60 lines (59 loc) · 1.52 KB
/
DeleteAlternate.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
package SummerTrainingGFG.LinkedList;
/**
* @author Vishal Singh
*/
public class DeleteAlternate {
static class Node {
int data;
Node next;
public Node(int data){
this.data = data;
}
}
static class LinkedList{
Node head;
Node tail;
void insertEnd(int data){
Node temp = new Node(data);
Node curr = head;
if (head == null){
head = temp;
tail = temp;
return;
}
while (curr.next != null){
curr=curr.next;
}
tail = temp;
curr.next = temp;
}
void insertEndArrayOfData(int[] data){
for (int i = 0; i < data.length; i++) {
insertEnd(data[i]);
}
}
void printList(){
if (head == null)
return;
Node curr = head;
while (curr != null){
System.out.print(curr.data+" ");
curr = curr.next;
}
System.out.println();
}
}
static void deleteAlternates(Node head){
Node slow = head;
while (slow != null && slow.next!= null){
slow.next = slow.next.next;
slow = slow.next;
}
}
public static void main(String[] args) {
LinkedList l = new LinkedList();
l.insertEndArrayOfData(new int[]{1,2,4,3,5,94,10});
deleteAlternates(l.head);
l.printList();
}
}