leetcode 删除链表的倒数第N个节点

208 / 208 个通过测试用例
状态:通过
执行用时:0 ms
内存消耗:34.8 MB

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
             ListNode curA = head;
        ListNode curB = head;
        for (int i = 0; i < n; i++) curA = curA.next;
        if (curA == null) {
            head = head.next;
            return head;
        }
        while (curA.next != null) {
            curA = curA.next;
            curB = curB.next;
        }
        curB.next = curB.next.next;
        return head;
}
}
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页