Created
October 13, 2017 01:26
-
-
Save shailrshah/eea3acd48cfa1e788a296b7010d4cc47 to your computer and use it in GitHub Desktop.
Given a linked list, reverse it.
This file contains hidden or 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
| //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