Skip to content

Instantly share code, notes, and snippets.

@BenMaydan
Last active January 30, 2022 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenMaydan/b5e627093b7b643fbf1782ec68d1205f to your computer and use it in GitHub Desktop.
Save BenMaydan/b5e627093b7b643fbf1782ec68d1205f to your computer and use it in GitHub Desktop.
Reverse a linked list
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
public static ListNode reverseList(ListNode l1) {
ListNode curr = ln;
ListNode prev = new ListNode(curr.val);
while (curr.next != null) {
prev = new ListNode(curr.next.val, prev);
curr = curr.next;
}
return prev;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment