## Problem :

You are given a Singly Linked List. You have to write a program to reverse a linked list and then print the reversed linked list.

Given a linked list of N nodes. The task is to reverse this list.

### Example 1:

Input:

Output: 6 5 4 3 2 1

Explanation: After reversing the list,

elements are 6->5->4->3->2->1.

### Example 2:

Input:

Output: 10 9 8 7 2

Explanation: After reversing the list,

elements are 10->9->8->7->2.

The task is to complete the function reverseList() with head reference as the only argument and should return a new head after reversing the list.

Expected Time Complexity: O(N).

Expected Auxiliary Space: O(1).

## Solutions

### Java Code:

```//Initial Template for Java

import java.util.*;
import java.io.*;

class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}

public class CB
{
Node lastNode;
static PrintWriter out;

/* Utility functions */

/* Inserts a new Node at front of the list. */
{

{
lastNode = node;
}
else
{
lastNode.next = node;
lastNode = node;
}
}
/* Function to print linked list */
void printList()
{
while (temp != null)
{
out.print(temp.data+" ");
temp = temp.next;
}
out.println();
}

public static void main(String args[]) throws IOException
{
out = new PrintWriter(new BufferedOutputStream(System.out));

while(t>0)
{

CB cb = new CB();
if (n > 0)
{
int a1= Integer.parseInt(nums[0]);
}
for (int i = 1; i < n; i++)
{
int a = Integer.parseInt(nums[i]);
}

cb.printList();

t--;
}
out.close();
}
}

// } Driver Code Ends
//function Template for Java

class Node {
int value;
Node next;
Node(int value) {
this.value = value;
}
} */

class ReverseLL
{
// This function should reverse linked list and return
{
{
}
else {
return rev;
}

}
}

```

### C++ Code:

```//Initial Template for C++

// C program to find n'th Node in linked list
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;

struct Node {
int data;
struct Node *next;
Node(int x)
{
data = x;
next = NULL;
}
};

// } Driver Code Ends

struct Node
{
int data;
struct Node *next;
}

*/

class Solution
{
public:
{
{
}
else {
return rev;
}
}

};

// { Driver Code Starts.

{
while (temp != NULL)
{
printf("%d ", temp->data);
temp  = temp->next;
}
}

/* Driver program to test above function*/
int main()
{
int T,n,l,firstdata;
cin>>T;

while(T--)
{
struct Node *head = NULL,  *tail = NULL;

cin>>n;

cin>>firstdata;

for (int i=1; i<n; i++)
{
cin>>l;
tail->next = new Node(l);
tail = tail->next;
}

Solution ob;