author: Blankj blog : http://blankj.com time : 2017/04/27 desc :
| 11 | * </pre> |
| 12 | */ |
| 13 | public class Solution { |
| 14 | public ListNode removeNthFromEnd(ListNode head, int n) { |
| 15 | ListNode pre = head; |
| 16 | ListNode afterPreN = head; |
| 17 | while (n-- != 0) { |
| 18 | afterPreN = afterPreN.next; |
| 19 | } |
| 20 | if (afterPreN != null) { |
| 21 | while (afterPreN.next != null) { |
| 22 | pre = pre.next; |
| 23 | afterPreN = afterPreN.next; |
| 24 | } |
| 25 | pre.next = pre.next.next; |
| 26 | } else { |
| 27 | head = head.next; |
| 28 | } |
| 29 | return head; |
| 30 | } |
| 31 | |
| 32 | public static void main(String[] args) { |
| 33 | Solution solution = new Solution(); |
| 34 | ListNode.print(solution.removeNthFromEnd(ListNode.createTestData("[1,2,3,4,5]"), 2)); |
| 35 | ListNode.print(solution.removeNthFromEnd(ListNode.createTestData("[1]"), 1)); |
| 36 | } |
| 37 | } |
nothing calls this directly
no outgoing calls
no test coverage detected