Created
December 30, 2013 08:25
-
-
Save wayetan/8179337 to your computer and use it in GitHub Desktop.
Remove Nth Node From End of 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
/** | |
* Given a linked list, remove the nth node from the end of list and return its head. | |
* For example, | |
* Given linked list: 1->2->3->4->5, and n = 2. | |
After removing the second node from the end, the linked list becomes 1->2->3->5. | |
* Given n will always be valid. | |
* Try to do this in one pass. | |
*/ | |
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } | |
*/ | |
public class Solution { | |
public ListNode removeNthFromEnd(ListNode head, int n) { | |
ListNode cur = head, prev = head; | |
while(n-- > 0) { | |
cur = cur.next; | |
} | |
if (cur == null) | |
return head.next; | |
while (cur.next != null) { | |
cur = cur.next; | |
prev = prev.next; | |
} | |
prev.next = prev.next.next; | |
return head; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment