leetcode---Remove Nth Node From End of List
2016-04-02 19:32
459 查看
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.
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. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { if (head == NULL) return NULL; ListNode *p = head, *q = head; int index = 0; while(index < n) { p = p->next; index++; } if(p == NULL) return head->next; else { while(p->next) { p = p->next; q = q->next; } } q->next = q->next->next; return head; } };
相关文章推荐
- Jade、EJS、JSHTML、Mustache、Handlebars 五大node模板引擎区别
- HDFS DataNode 设计实现解析
- HDFS NameNode 设计实现解析
- Node.js进程管理之Process模块
- 初识Node.js
- nodejs pm2的简单应用
- 利用socket.io实现多人聊天室(基于Nodejs)
- 33.leetcode题目237: Delete Node in a Linked List
- leetcode019 Remove Nth Node From End of List
- NodeJS、NPM安装配置步骤(windows版本)
- node.js环境搭建——helloworld
- Ubuntu里node命令无效解决方法
- node.js Stream
- node-firefox 二三事
- node-firefox 二三事
- 基于Node.js的微服务后台
- (转)如何制作nodejs,npm “绿色”安装包
- node.js开发中使用Node Supervisor实现监测文件修改并自动重启应用
- LintCode "Swap Two Nodes in Linked List"
- nodejs笔记四--创建一个最简单的 express 应用