Last active
August 29, 2015 13:58
-
-
Save gabhi/9985846 to your computer and use it in GitHub Desktop.
reverse linked list
This file contains 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
private static ListNode reverseList(ListNode head) { | |
ListNode current = head; | |
if (current == null) | |
return null; | |
if (current.next == null) | |
return current; | |
ListNode nextItem = current.next; | |
current.next = null; | |
ListNode remaining = reverseList(nextItem); | |
nextItem.next = current; | |
return remaining; | |
} | |
class ListNode { | |
public int value; | |
public ListNode next; | |
public ListNode(int value) { | |
this.value = value; | |
this.next = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment