Skip to content

Instantly share code, notes, and snippets.

@shailrshah
Created October 13, 2017 01:26
Show Gist options
  • Select an option

  • Save shailrshah/eea3acd48cfa1e788a296b7010d4cc47 to your computer and use it in GitHub Desktop.

Select an option

Save shailrshah/eea3acd48cfa1e788a296b7010d4cc47 to your computer and use it in GitHub Desktop.
Given a linked list, reverse it.
//public class ListNode {
// int val;
// ListNode next;
// ListNode(int x) { val = x; }
//}
public ListNode reverseList(ListNode head) {
ListNode curr = head;
ListNode prev = null, next = null;
while(curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment