Reverse Linked List Problem Statement
Given a Singly Linked List of integers, your task is to reverse the Linked List by altering the links between the nodes.
Input:
The first line of input is an integer T, representing the number of test cases.
For each test case, the first line contains the elements of the singly linked list, separated by a space, and terminated by -1. The value -1 is not part of the list itself.
Output:
For each test case, output the reversed version of the linked list. The elements should be space-separated, ending with -1.
Provide the output for each test case on a new line.
Example:
Input:
2
1 2 3 4 5 -1
10 20 30 40 -1
Output:
5 4 3 2 1 -1
40 30 20 10 -1
Constraints:
1 <= T <= 10
1 <= N <= 10^4
0 <= data <= 10^9
Where 'N' represents the number of nodes in the linked list.
Time Limit: 1 sec
Note:
Return the head of the reversed linked list; printing is not required.
Be the first one to answer
Add answer anonymously...
Top Cisco SDE-2 interview questions & answers
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app