[LeetCode]Remove Nth Node From End of List

public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        head = dummy;
        ListNode pre = head;
        ListNode p = pre.next;
        for (int i = 0; i < n; i++) {
            p = p.next;
        }
        while (p != null) {
            p = p.next;
            pre = pre.next;
        }
        pre.next = pre.next.next;
        return head.next;
    }
}

 

posted @ 2015-12-01 15:35  Weizheng_Love_Coding  阅读(97)  评论(0编辑  收藏  举报