资讯专栏INFORMATION COLUMN

leetcode 19. Remove Nth Node From End of List

bigdevil_s / 805人阅读

摘要:这题也是携程年暑假实习生的笔试题。最开始想的解法就是,先循环求链表的长度,再用长度,再循环一次就能移除该结点。结果对的,但是超时了。再返回整个链表。

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.

这题也是携程18年暑假实习生的笔试题。

最开始想的解法就是,先循环求链表的长度,再用长度-n,再循环一次就能移除该结点。结果对的,但是超时了。代码如下:

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
var removeNthFromEnd = function(head, n) {
    let start = new ListNode(null);
    start.next = head;
    let len = 0,
        current = null,
        del = null;
    if(head === null){
        len = 0;
    }else{
        current = head;
        len = 1;
        while(current.next){
            len++;
        }
    }
    let position = len - n;
    if(position > -1 && position < len){
        current = head;
        let previous,
            index = 0;
        // 移除第一项
        if(position === 0){
            head = current.next;
        }else{
            while(index < position){
                previous = current;
                current = current.next;
                index++;
            }
            del = current;
            previous.next = current.next;
        }
        len--;
        return start.next;
    }else{
        return null;
    }
};

后来看了别人的最佳解法,觉得自己太蠢了。
最佳解法:用两个指针,第一个指针slow的next指向第一个节点,第二个指针fast指向第n+1个结点,然后两个指针同时后移,直到fast指针指向null,这个时候slow指针指向要删除结点的前一个节点,使用slow.next = slow.next.next删除结点。再返回整个链表。代码如下:

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
var removeNthFromEnd = function(head, n) {
    var start = new ListNode(null);
    start.next = head;
    let slow = start,
        fast = head;
    slow.next = head;
    
    if(head.next === null){
        return null
    }
    
    for(var i = 0; i < n; i++){
        fast = fast.next;
    }
    
    while(fast !== null){
        slow = slow.next;
        fast = fast.next;
    }
    
    slow.next = slow.next.next;
    return start.next;
};

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

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

相关文章

  • leetcode 19 Remove Nth Node From End of List

    摘要:题目详情题目要求输入一个和一个数字。要求我们返回删掉了倒数第个节点的链表。想法求倒数第个节点,我们将这个问题转化一下。我们声明两个指针和,让和指向的节点距离差保持为。解法使点和点的差距为同时移动和使得到达的末尾删除倒数第个节点 题目详情 Given a linked list, remove the nth node from the end of list and return it...

    chaosx110 评论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
  • leetcode-019-删除链表倒数第N个结点(Remove Nth Node From End

    摘要:第题给定一个链表,删除链表的倒数第个节点,并且返回链表的头结点。因为,若有一个真正的头结点,则所有的元素处理方式都一样。但以第一个有效元素为头结点,就导致算法的不一致,需要单独处理第一个有效元素头结点。 leetcode第19题 Given a linked list, remove the n-th node from the end of list and return its h...

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

发表评论

0条评论

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