[LeetCode]--19. Remove Nth Node From End of List

简介: 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 fr

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.

Note:
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; }
 * }
 */
public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null)
            return null;
        if (head.next == null)
            return null;
        ListNode p1, p2;
        int flag = 1;
        p1 = p2 = head;
        for (int i = 0; i < n; i++) {
            if (p2.next != null)
                p2 = p2.next;
            else
                flag = 0;
        }
        if (flag == 0) {
            head = head.next;
        } else {
            while (p2.next != null) {
                p1 = p1.next;
                p2 = p2.next;
            }
            p1.next = p1.next.next;
        }
        return head;
    }
}

我提交通过了,这里精髓就是利用双指针一次遍历就能做到,flag必不可少,因为删除的时候必须知道要删除的地方的上一个节点,这样一来,如果是删除头结点,那么会造成p2空指针异常,所以我们得设置一个flag标记一下。

提交成功之后又可以看别人优秀代码。
Approach #1 (Two pass algorithm)

public ListNode removeNthFromEnd(ListNode head, int n) {
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    int length  = 0;
    ListNode first = head;
    while (first != null) {
        length++;
        first = first.next;
    }
    length -= n;
    first = dummy;
    while (length > 0) {
        length--;
        first = first.next;
    }
    first.next = first.next.next;
    return dummy.next;
}

这就是典型的遍历了两次链表才得到数据。

Approach #2 (One pass algorithm)
这里写图片描述

public ListNode removeNthFromEnd(ListNode head, int n) {
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode first = dummy;
    ListNode second = dummy;
    // Advances first pointer so that the gap between first and second is n nodes apart
    for (int i = 1; i <= n + 1; i++) {
        first = first.next;
    }
    // Move first to the end, maintaining the gap
    while (first != null) {
        first = first.next;
        second = second.next;
    }
    second.next = second.next.next;
    return dummy.next;
}

思想跟我的是一样的,但是经过仔细推敲,他的这种处理头结点的办法比我那个可取,值得学习。

LeetCode官方指南

目录
相关文章
|
8月前
|
JavaScript
DOM 节点列表长度(Node List Length)
`length`属性用于获取DOM节点列表的长度,即节点数量。通过遍历这个属性,可以访问和处理所有节点。例如,示例代码加载&quot;books.xml&quot;,获取所有&quot;title&quot;节点,并依次输出它们的第一个子节点的值。
|
9月前
|
XML JavaScript 数据格式
DOM 节点列表长度(Node List Length)
`length`属性用于获取DOM节点列表的元素数量。在示例中,代码加载&quot;books.xml&quot;,然后通过`getElementsByTagName(&quot;title&quot;)`获取所有标题节点。使用`for`循环遍历这些节点,输出每个标题的文本内容。这个例子展示了如何交互式地处理XML文档中的特定标签。
|
9月前
|
JavaScript
DOM 节点列表长度(Node List Length)
`length`属性用于获取DOM节点列表的元素数量。通过遍历这个属性,如`for (i=0;i&lt;x.length;i++)`,可以访问和处理每个节点。在示例中,代码加载&quot;books.xml&quot;,然后获取所有&quot;title&quot;标签,并打印它们的子节点值。
|
8月前
|
JavaScript
DOM 节点列表长度(Node List Length)
`length`属性用于获取DOM节点列表的元素数量。在示例中,代码加载&quot;books.xml&quot;,然后通过`getElementsByTagName(&quot;title&quot;)`获取所有标题节点。使用`for`循环遍历这些节点,输出每个标题的文本内容。
|
9月前
|
JavaScript
DOM 节点列表长度(Node List Length)
`length`属性用于获取DOM节点列表的长度。在示例中,代码加载&quot;books.xml&quot;,然后使用`getElementsByTagName(&quot;title&quot;)`获取所有标题节点。通过循环`x.length`,遍历并输出每个标题节点的第一个子节点的值。
|
8月前
|
XML JavaScript 数据格式
DOM 节点列表长度(Node List Length)
`length`属性表示DOM节点列表的长度。在示例中,通过加载&quot;books.xml&quot;到`xmlDoc`,并使用`getElementsByTagName(&quot;title&quot;)`获取所有标题节点,然后利用`for`循环遍历整个节点列表,每次迭代通过`childNodes[0].nodeValue`访问每个节点的第一个子节点的值并输出。此方法可用于处理XML或HTML文档中的节点列表。 **Markdown格式:** `length`属性表示DOM节点列表的长度。
|
8月前
|
JavaScript
DOM 节点列表长度(Node List Length)
`length`属性定义了节点列表的长度(即节点数量)。可通过此属性遍历节点列表。
|
9月前
|
JavaScript
DOM 节点列表长度(Node List Length)
`length`属性用于获取DOM节点列表的元素数量。通过它,可以迭代列表,如示例所示:加载&quot;books.xml&quot;,然后获取所有&quot;title&quot;节点。循环`x.length`,打印每个标题节点的第一个子节点的值。
|
9月前
|
JavaScript
DOM 节点列表长度(Node List Length)
`length`属性用于获取DOM节点列表的元素数量。通过遍历这个属性,如`for (i=0; i&lt;x.length; i++)`,可以访问和处理每个节点。在示例中,代码加载&quot;books.xml&quot;,然后获取所有&quot;title&quot;节点,并打印它们的第一个子节点的值。
|
9月前
|
XML JavaScript 数据格式
DOM 节点列表长度(Node List Length)
`length`属性用于获取DOM节点列表的长度,即节点数量。通过它可遍历列表,如`for(i=0; i&lt;x.length; i++)`循环访问每个`title`节点,并输出其内容。示例展示了从&quot;books.xml&quot;加载XML后,获取并打印所有标题节点的值。

热门文章

最新文章