MCPcopy Create free account
hub / github.com/ashishps1/awesome-leetcode-resources / ReverseLinkedList

Class ReverseLinkedList

patterns/java/ReverseLinkedList.java:8–20  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6}
7
8public class ReverseLinkedList {
9 public ListNode reverseList(ListNode head) {
10 ListNode prev = null; // Previous node, initially null
11 ListNode curr = head; // Current node starts from the head
12 while (curr != null) {
13 ListNode next = curr.next; // Store next node
14 curr.next = prev; // Reverse the current node's pointer
15 prev = curr; // Move prev to current
16 curr = next; // Move curr to next
17 }
18 return prev; // New head of the reversed list
19 }
20}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected