/ListNode.java Secret
Created
June 7, 2017 22:39
Star
You must be signed in to star a gist
Reverse a singly 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
package com.interviewbytes.linkedlists; | |
public class ListNode { | |
int val; | |
ListNode next; | |
ListNode(int x) { | |
val = x; | |
} | |
} |
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
package com.interviewbytes.linkedlists; | |
public class ReverseLinkedList { | |
public ListNode reverseList(ListNode head) { | |
ListNode reversed = null; | |
while (head != null) { | |
ListNode next = head.next; | |
head.next = reversed; | |
reversed = head; | |
head = next; | |
} | |
return reversed; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment