Skip to content

Instantly share code, notes, and snippets.

@InterviewBytes
Created June 7, 2017 22:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save InterviewBytes/8d6939c8b0f5628758b0f8bf481a0e1a to your computer and use it in GitHub Desktop.
Reverse a singly linked list.
package com.interviewbytes.linkedlists;
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
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