import
java.util.*;
class
node
{
int
data;
node next;
node(
int
key)
{
data = key;
next =
null
;
}
}
class
GFG
{
public
static
node reverseLL(node root)
{
node prev =
null
,
current = root, next;
while
(current!=
null
)
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
root = prev;
return
root;
}
public
static
void
modifyLL( node root)
{
node slow_ptr = root, fast_ptr = root;
while
(fast_ptr !=
null
&&
fast_ptr.next !=
null
)
{
fast_ptr = fast_ptr.next.next;
slow_ptr = slow_ptr.next;
}
node root2 = reverseLL(slow_ptr.next);
slow_ptr.next =
null
;
node current1 = root,
current2 = root2;
while
(current1 !=
null
&&
current2 !=
null
)
{
node dnext1 = current1.next;
node dnext2 = current2.next;
current1.next = current2;
current2.next = dnext1;
current1 = dnext1;
current2 = dnext2;
}
}
public
static
node insertNode(node start,
int
val)
{
node temp =
new
node(val);
if
(start ==
null
)
start = temp;
else
{
node dstart = start;
while
(dstart.next !=
null
)
dstart = dstart.next;
dstart.next = temp;
}
return
start;
}
public
static
void
display(node start)
{
node temp = start;
while
(temp.next !=
null
) {
System.out.print(temp.data +
"->"
);
temp = temp.next;
}
System.out.println(temp.data);
}
public
static
void
main(String args[])
{
node start =
null
;
start = insertNode(start,
1
);
start = insertNode(start,
2
);
start = insertNode(start,
3
);
start = insertNode(start,
4
);
start = insertNode(start,
5
);
System.out.print(
"Before Modifying: "
);
display(start);
modifyLL(start);
System.out.print(
"After Modifying: "
);
display(start);
start =
null
;
start = insertNode(start,
1
);
start = insertNode(start,
2
);
start = insertNode(start,
3
);
start = insertNode(start,
4
);
start = insertNode(start,
5
);
start = insertNode(start,
6
);
System.out.print(
"Before Modifying: "
);
display(start);
modifyLL(start);
System.out.print(
"After Modifying: "
);
display(start);
}
}