Given a linked list of N nodes. The task is to reverse the list by changing links between nodes (i.e if the list is 1->2->3->4 then it becomes 1<-2<-3<-4) and return the head of the modified list.
Input
User Task:
Since this will be a functional problem, you don't have to take input. You just have to complete the function ReverseLinkedList that takes head node as parameter.
Constraints:
1 <=N <= 1000
1 <= Node.data<= 100
Output
Return the head of the modified linked list.
public class Main {
public static Node reverseLinkedList(Node head) {
Node newHead = null;
while (head != null) {
Node tmp = new Node(head.data);
tmp.next = newHead;
newHead = tmp;
head = head.next;
}
return newHead;
}
}
Comments
Leave a comment