class
GFG
{
static
class
Node
{
char
data;
Node next;
};
static
Node newNode(
char
data)
{
Node temp =
new
Node();
temp.data = data;
temp.next =
null
;
return
temp;
}
static
void
append(Node head_ref,
char
new_data)
{
Node new_node = newNode(new_data);
Node last = head_ref;
if
(head_ref ==
null
)
{
head_ref = new_node;
return
;
}
while
(last.next !=
null
)
last = last.next;
last.next = new_node;
return
;
}
static
void
printList(Node node)
{
while
(node !=
null
)
{
System.out.print(node.data+
" "
);
node = node.next;
}
}
static
void
RLE(Node head)
{
Node p = head;
Node temp = newNode(p.data);
char
c = p.data;
p = p.next;
int
count =
1
;
while
(p !=
null
)
{
char
x = p.data;
if
(c == x)
count++;
else
{
if
(count >
1
)
{
if
(count >
9
)
append(temp, (
char
) (
'0'
+ (count /
10
)));
append(temp, (
char
) (
'0'
+ (count %
10
)));
}
count =
1
;
append(temp, x);
c = x;
}
p = p.next;
}
if
(count !=
0
)
append(temp, (
char
) (
'0'
+ count));
printList(temp);
}
public
static
void
main(String[] args)
{
Node head = newNode(
'a'
);
head.next = newNode(
'a'
);
head.next.next = newNode(
'a'
);
head.next.next.next = newNode(
'b'
);
head.next.next.next.next = newNode(
'r'
);
head.next.next.next.next.next = newNode(
'r'
);
RLE(head);
}
}