-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathfoldALinkedList.java
144 lines (118 loc) · 3.44 KB
/
foldALinkedList.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
/*
The function is expected to place last element after 1st element, 2nd last element after 2nd element and so on.
Example 1
1->2->3->4->5
will fold as
1->5->2->4->3
Implement on singly Linked List , using Recurrsion.
*/
import java.io.*;
import java.util.*;
public class Main {
// Creating Node for Singly Linked List
public static class Node {
int data;
Node next;
}
// singly Linked List
public static class LinkedList {
Node head;
Node tail;
int size;
void addLast(int val) {
Node temp = new Node();
temp.data = val;
temp.next = null;
if (size == 0) {
head = tail = temp;
} else {
tail.next = temp;
tail = temp;
}
size++;
}
// size function
public int size() {
return size;
}
// display function
public void display() {
for (Node temp = head; temp != null; temp = temp.next) {
System.out.print(temp.data + " ");
}
System.out.println();
}
// static variable for ending purpose
static int c;
// static left pointer
static Node left;
// helper function
private void foldHelper(Node node) {
// If reached null node simply return
if(node==null)
{
return;
}
// else call helper function for next node for placing right pointer at the tail
foldHelper(node.next);
// c works as flag to check if left pointer and right pointer has crosssed each other
if(c==1)
{
return;
}
// if left.next = right node or left == right node
else if(left.next==node || left==node)
{
// this node will be our tail node
node.next=null;
tail=node;
// shows our work is complete now
c=1;
return;
}
// if work left pointer has not crossed right pointer
else
{
// storing the next of left since our new next will be right pointer
Node nbr = left.next;
// storing right pointer in the next of left
left.next=node;
// this can we visualized as simple inserting right pointer between left pointer and its next
node.next=nbr;
// our new left will be its previous next
left=nbr;
}
}
// Fold function
public void fold() {
c=0;
// initially left pointer will be at head
left=head;
// calling helper function for result
foldHelper(head);
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n1 = Integer.parseInt(br.readLine());
LinkedList l1 = new LinkedList();
String[] values1 = br.readLine().split(" ");
for (int i = 0; i < n1; i++) {
int d = Integer.parseInt(values1[i]);
l1.addLast(d);
}
int a = Integer.parseInt(br.readLine());
int b = Integer.parseInt(br.readLine());
l1.display();
l1.fold();
l1.display();
}
}
/*
Sample Input/Output:
Input: 5
1 2 3 4 5
Output: 1 5 2 4 3
Time Complexity: O(n)
Space Complexity: O(n)
*/