19. Remove Nth Node From End of List

标签: 链表
3人阅读 评论(0) 收藏 举报
分类:

题目描述

Given a linked list, remove the n-th node from the end of list and return its head.

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.

Follow up:

Could you do this in one pass?

解题代码

 class Solution {
 public:
     ListNode* removeNthFromEnd(ListNode* head, int n) {
         if (head == nullptr || head->next == nullptr)
             return nullptr;
         int step = 1;
         ListNode * first = head, *second = head, *parent = head;
         while (step++ < n)
         {
             first = first->next;
         }
         if (first->next == nullptr) {
             head = head->next;
             delete[] second;
             return head;
         }
         while (first->next != nullptr)
         {
             first = first->next;
             parent = second;
             second = second->next;
         }
         parent->next = second->next;
         delete[] second;
         return head;
     }
 };

解题思路

  1. 最常见的方法是遍历两遍链表,第一遍得到链表的节点数目,第二遍是找到指定位置的链表。
  2. 可以做到只遍历一遍链表,但需要两个指针。因为要删除的是倒数第n个节点,那我们让两个指针指向的节点相差n个,那么当前面的指针指向链表尾节点时,后面的指针指向的节点就是我们想要删除的节点。
  3. 由于我们要删除节点时,需要拿到的节点是待删除节点的父节点。初始化时我们让父亲节点指针和删除节点指针指向头节点。先判断两个指针相差n时前面的指针是不是已经指向尾节点了,是的话删除的就是头节点。否则就循环至尾节点,使用父节点指针删除待删除节点。
查看评论

From Microservices to Data Microservices

-
  • 1970年01月01日 08:00

[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)

[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)
  • hcbbt
  • hcbbt
  • 2015-03-04 19:26:10
  • 2374

[leetcode] 19. Remove Nth Node From End of List python实现【easy】

Remove Nth Node From End of List My Submissions QuestionEditorial Solution Given a linked list, r...
  • zl87758539
  • zl87758539
  • 2016-06-14 21:50:21
  • 805

[C语言][LeetCode][19]Remove Nth Node From End of List

题目Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list an...
  • Timsley
  • Timsley
  • 2016-03-07 23:05:04
  • 971

LeetCode 19 : Remove Nth Node From End of List (Java)

解题思路:一开始我的做法是先遍历一遍取得长度,然后长度减去n就知道要删除正着数第几个元素,结果题目要求one pass。那就只能用两个指针,一个快指针先走n步,一个慢指针从头开始走,这样当快指针走到尾...
  • changetocs
  • changetocs
  • 2015-12-01 20:02:39
  • 1029

(Java)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, ...
  • u012848330
  • u012848330
  • 2016-05-31 00:00:58
  • 305

【LeetCode-面试算法经典-Java实现】【019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)】

【019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a l...
  • DERRANTCM
  • DERRANTCM
  • 2015-07-22 07:24:29
  • 2296

LeetCode: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...
  • yao_wust
  • yao_wust
  • 2014-11-18 10:08:28
  • 4038

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, ...
  • booirror
  • booirror
  • 2015-02-05 15:07:55
  • 1240

【LeetCode】19 Remove Nth Node From End of List (c++实现)

Given a linked list, remove the nth node from the end of list and return its head. For example,    G...
  • styshoo
  • styshoo
  • 2015-07-22 10:29:58
  • 411
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 1万+
    积分: 1399
    排名: 3万+
    博客专栏
    最新评论