博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Remove Nth Node From End of List
阅读量:7101 次
发布时间:2019-06-28

本文共 2366 字,大约阅读时间需要 7 分钟。

This is a classic problem of linked list. You may solve it using two pointers. The tricky part lies in the head pointer may also be the one that is required to be removed. Handle this carefully.

1 class Solution { 2 public: 3     ListNode *removeNthFromEnd(ListNode *head, int n) { 4         ListNode *pre, *cur; 5         pre = cur = head; 6         int i = 0; 7         for(; i < n; i++) 8             cur = cur -> next; 9         if(!cur) return head -> next;10         while(cur -> next) {11             pre = pre -> next;12             cur = cur -> next;13         }14         pre -> next = pre -> next -> next;15         return head;16     }17 };

You may create a dummy that points to head to facilitate the removal.

1 class Solution { 2 public: 3     ListNode* removeNthFromEnd(ListNode* head, int n) { 4         ListNode* dummy = new ListNode(0); 5         dummy -> next = head; 6         ListNode* pre = dummy; 7         ListNode* cur = dummy; 8         for (int i = 0; i < n; i++) 9             cur = cur -> next;10         while (cur -> next) {11             pre = pre -> next;12             cur = cur -> next;13         }14         ListNode* del = pre -> next;15         pre -> next = del -> next;16         delete del;17         return dummy -> next;18     }19 };

has a much shorter solution using pointers to pointers, which is a little difficult to understand. The code is rewritten below.

1 class Solution { 2 public: 3     ListNode* removeNthFromEnd(ListNode* head, int n) { 4         ListNode** pre = &head; 5         ListNode* cur = head; 6         for (int i = 1; i < n; i++) 7             cur = cur -> next; 8         while (cur -> next) { 9             pre = &((*pre) -> next);10             cur = cur -> next;11         }12         *pre = (*pre) -> next;13         return head;14     }15 };

There is also a recursive solution to this problem in the answers in the above link. 

1 class Solution { 2 public: 3     ListNode* removeNthFromEnd(ListNode* head, int n) { 4         return countRemove(head, n) == 0 ? head -> next : head; 5     } 6 private: 7     int countRemove(ListNode* node, int n) { 8         if (!(node -> next)) return n - 1; 9         int rest = countRemove(node -> next, n);10         if (!rest) node -> next = node -> next -> next;11         return rest - 1;12     }13 };

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4628541.html

你可能感兴趣的文章
springboot activiti 整合项目框架源码 shiro 安全框架 druid 数据库连接池
查看>>
Java开发必须要掌握的21个核心技术
查看>>
数据结构与算法的重温之旅(一)——复杂度分析
查看>>
debounce防抖函数之lodash
查看>>
Spring Cloud微服务分布式云架构 - 整合企业架构的技术点
查看>>
5分钟 0元搭建个人独立博客网站 Hexo+Github pages
查看>>
一个 react 的小项目方便查看 github 上的笔记
查看>>
当无人编辑坐镇新闻编辑部?未来人机社会共存指南
查看>>
git命令
查看>>
JavaScript是如何工作的:渲染引擎和优化其性能的技巧
查看>>
1017 A除以B (20 分)
查看>>
Grid布局简介
查看>>
android 微信、支付宝支付踩坑之旅
查看>>
数据库监控
查看>>
International SEO:多语言多区域网站SEO的快速入门指南
查看>>
tty的求助
查看>>
钱包开发数字货币钱包开发虚拟币多币种钱包开发
查看>>
spring cloud互联网分布式微服务云平台规划分析--spring cloud服务统一配置中心
查看>>
【Flask】关于request.json /.values /.args /.form
查看>>
Virtualbox虚拟Ubuntu共享文件夹设置
查看>>