From 94c0e54863be71ec81c2409de5c4b2600063bfe6 Mon Sep 17 00:00:00 2001 From: Colston Bod-oy <75562733+ColstonBod-oy@users.noreply.github.com> Date: Sat, 22 Apr 2023 20:19:32 +0800 Subject: [PATCH] Update 0206-reverse-linked-list.java Made the variable names more descriptive for the iterative solution. --- java/0206-reverse-linked-list.java | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/java/0206-reverse-linked-list.java b/java/0206-reverse-linked-list.java index 1a03e7cf5..18e332dae 100644 --- a/java/0206-reverse-linked-list.java +++ b/java/0206-reverse-linked-list.java @@ -3,16 +3,18 @@ class Solution { public ListNode reverseList(ListNode head) { - ListNode p = null; - ListNode q = null; - ListNode r = head; - while (r != null) { - p = q; - q = r; - r = r.next; - q.next = p; + ListNode current = head; + ListNode previous = null; + ListNode nextCurrent = null; + + while (current != null) { + nextCurrent = current.next; + current.next = previous; + previous = current; + current = nextCurrent; } - return q; + + return previous; } }