You are given a Singly Linked List of integers. You need to reverse the Linked List by changing the links between nodes.
Example:
Input:
2 4 5 -1
Output:
5 4 2 -1
Explanation: 2->4->5 is the initial linked list. If we reverse this, we get 5->4->2.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains the elements of the singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
Output Format :
Print the reversed linked list. The elements of the reversed list should be single-space separated, terminated by -1.
Note :
You do not need to print anything, just return the head of the reversed linked list.
Sample Input 1 :
1 2 4 -1
Sample Output 1 :
4 2 1 -1
Explanation for Sample Input 1 :
1->2->4 is the initial linked list. If we reverse this, we get 4->2->1.
Sample Input 2 :
1
1 1 1 -1
Sample Output 2 :
1 1 1 -1
Constraints :
1 <= 'N' <= 10^4
0 <= 'data' <= 10^9
Where 'N' is the number of nodes in the linked list.
Time Limit: 1 sec