资讯专栏INFORMATION COLUMN

leetcode-019-删除链表倒数第N个结点(Remove Nth Node From End

brianway / 2372人阅读

摘要:第题给定一个链表,删除链表的倒数第个节点,并且返回链表的头结点。因为,若有一个真正的头结点,则所有的元素处理方式都一样。但以第一个有效元素为头结点,就导致算法的不一致,需要多带带处理第一个有效元素头结点。

leetcode第19题

Given a linked list, remove the n-th node from the end of list and return its head.
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。

题中的坑

这个题要注意的是:网站定义的链表结构head指向第一个有效元素,没有纯正意义上的头结点,我前两次提交就是因为这个问题没过。因为,若有一个真正的头结点,则所有的元素处理方式都一样。但以第一个有效元素为头结点,就导致算法的不一致,需要多带带处理第一个有效元素(头结点)。

题目的额外限制

Could you do this in one pass?
你能尝试使用一趟扫描实现吗?

还好,题目约束,给定n值一定会是有效的(Given n will always be valid.),这可以少写好多的边界判断。

我的解答(javascript) 思路

n值一定有效,又限定在一趟扫描过程中完成,那就是要在扫描的过程中,保存删除操作的所有信息。很容易想到,链表的长度一定大于n,我们迭代处理的是当前元素,故只需要记住当前元素往前的第n+1个元素(即被删除元素的前一个)就可以了。

链接结点定义
function ListNode(val) {
    this.val = val
    this.next = null;
}
单链接生成器(用于本地测试)
function build(arr) {
    if(arr.length === 0)         //注意:leetcode的空链表逻辑是head=null
        return null
    let rs = new ListNode(arr[0])
    let head = rs
    for(let i = 1; i < arr.length; i++) {
        let newOne = new ListNode(arr[i])
        rs.next = newOne
        rs = newOne
    }
    return head
}
本地测试代码
    let rs = removeNthFromEnd(build([1,2,3,4,5]), 2)
    let cur = rs
    let str = ""
    while(cur !== null) {
        str += `${cur.val} ${cur.next ? "->" : ""} `
        cur = cur.next
    }
    console.log(str)
解答算法
var removeNthFromEnd = function(head, n) {
    let cur = head        //迭代处理当前元素
    let m = 0             //偏移量,用来指示要删除的元素
    let del = head        //要删除的元素
    while (cur !== null) {
        if(m > n) {         //达到并偏移指示窗口
            del = del.next
        } else {
            m++
        }
        cur = cur.next
    }
    if (del === head && m === n)  //注意,删除头元素与其它元素是不一样的
        head = head.next          //测试用例:[1] 1; [1,2] 2
    else 
        del.next = del.next.next
    return head
};
leetcode提交结果

Runtime: 56 ms, faster than 100.00% of JavaScript online submissions for Remove Nth Node From End of List.

我的第一个运行速度超过所有提交者的解答,^_^

完整代码
https://github.com/zhoutk/leetcode/blob/master/javascript/qs019_removeNthFromEnd_1.js
小结

本文主要标记leetcode中的链表结构的特殊性,head直接指向第一个有效元素。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/100803.html

相关文章

  • LeetCode 19:删除链表倒数N节点 Remove Nth Node From End

    摘要:给定一个链表,删除链表的倒数第个节点,并且返回链表的头结点。示例给定一个链表和当删除了倒数第二个节点后,链表变为说明给定的保证是有效的。值得注意的的是,指向应当删除的节点并无法删除它,应当指向该删除节点的前一个节点。 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。 Given a linked list, remove the n-th node from the ...

    qiangdada 评论0 收藏0
  • LeetCode 19:删除链表倒数N节点 Remove Nth Node From End

    摘要:给定一个链表,删除链表的倒数第个节点,并且返回链表的头结点。示例给定一个链表和当删除了倒数第二个节点后,链表变为说明给定的保证是有效的。值得注意的的是,指向应当删除的节点并无法删除它,应当指向该删除节点的前一个节点。 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。 Given a linked list, remove the n-th node from the ...

    周国辉 评论0 收藏0
  • [LeetCode/LintCode] Remove Nth Node From End of L

    Problem Given a linked list, remove the nth node from the end of list and return its head. Example Given linked list: 1->2->3->4->5->null, and n = 2. After removing the second node from the end, the l...

    Jaden 评论0 收藏0
  • 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...

    bigdevil_s 评论0 收藏0
  • leetcode19 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, ...

    YPHP 评论0 收藏0

发表评论

0条评论

brianway

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<