Skip to content

Instantly share code, notes, and snippets.

@ugandapinik
Created July 17, 2021 14:24
Show Gist options
  • Save ugandapinik/2cea8721b55d37634d347d8a681eef32 to your computer and use it in GitHub Desktop.
Save ugandapinik/2cea8721b55d37634d347d8a681eef32 to your computer and use it in GitHub Desktop.
public static SinglyLinkedListNode reverse(SinglyLinkedListNode llist) {
// Write your code here
SinglyLinkedListNode curr = llist;
SinglyLinkedListNode next = llist.next;
llist.next = null; // 1->null
while(next != null){
SinglyLinkedListNode temp = next.next; // 3->4->5
next.next = curr; // 2->1->null
curr = next; // 2->1->null;
next = temp;
}
return curr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment