...

/

Reverse a LinkedList (easy)

Reverse a LinkedList (easy)

Problem Statement #

Given the head of a Singly LinkedList, reverse the LinkedList. Write a function to return the new head of the reversed LinkedList.

Try it yourself #

Try solving this question here:

class ListNode {
int value = 0;
ListNode next;
ListNode(int value) {
this.value = value;
}
}
class ReverseLinkedList {
public static ListNode reverse(ListNode head) {
// TODO: Write your code here
return head;
}
public static void main(String[] args) {
ListNode head = new ListNode(2);
head.next = new ListNode(4);
head.next.next = new ListNode(6);
head.next.next.next = new ListNode(8);
head.next.next.next.next = new ListNode(10);
ListNode result = ReverseLinkedList.reverse(head);
System.out.print("Nodes of the reversed LinkedList are: ");
while (result != null) {
System.out.print(result.value + " ");
result = result.next;
}
}
}

Solution #

...