Skip to content

Instantly share code, notes, and snippets.

@lpabon
Created April 4, 2022 00:21
Show Gist options
  • Save lpabon/57ecab9158fae7e7e185b8bcd8763f2b to your computer and use it in GitHub Desktop.
Save lpabon/57ecab9158fae7e7e185b8bcd8763f2b to your computer and use it in GitHub Desktop.
Reverse a linked list without using new in java
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