Answer to Question #300241 in Java | JSP | JSF for Akash srivastava

Question #300241

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.


1
Expert's answer
2022-02-20T09:22:22-0500
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;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS