Created
April 4, 2022 00:21
-
-
Save lpabon/57ecab9158fae7e7e185b8bcd8763f2b to your computer and use it in GitHub Desktop.
Reverse a linked list without using new in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Node reverse(Node list) { | |
Node n; | |
Node head; | |
if (list == null) { | |
return null; | |
} | |
head = list; | |
n = list; | |
while (n != null) { | |
list = list.next; | |
if (n == head) { | |
n.next = null; | |
} else { | |
n.next = head; | |
} | |
head = n; | |
n = list; | |
} | |
return head; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment