LeetCode OJ:Remove Nth Node From End of List(倒序移除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->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.
只能走一遍,要求移除倒着数的第n个元素,开始想用递归做,但是一直失败,没办法看了一下别人的答案,如果总数为N那么倒着第n个相当于正着第N-n个,N的计数通过一次遍历计数即相对的可以得到,代码如下:
/**
* 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) {
ListNode * p, * q, * pPre;
pPre = NULL;
p = q = head;
while(--n > )
q = q->next;
while(q->next){
pPre = p;
p = p->next;
q = q->next; }
if(pPre == NULL){
head = p->next;
delete p;
}else{
pPre->next = p->next;
delete p;
}
return head;
}
};
LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)的更多相关文章
- LeetCode 019 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 ...
- leetcode 【 Remove Nth Node From End of List 】 python 实现
题目: Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 【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 example ...
- 【leetcode】Remove Nth Node From End of List(easy)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- [leetcode 19] Remove Nth Node From End of List
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- 【JAVA、C++】LeetCode 019 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, Give ...
- 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 ...
- 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, Give ...
随机推荐
- mysql 获取随机10条数据
SELECT * FROM s_user WHERE id>= ((SELECT MAX(id) FROM s_user)-(SELECT MIN(id) FROM s_user)) * RAN ...
- pandas(六)读写文本格式的数据
pandas提供的将表格型数据读取为DataFrame对象的函数. 函数 说明 read_csv 从文件.URL.文件型对象中加载带分隔符的数据.默认分隔符为逗号. read_table 从文件.UR ...
- 使用反射实现 webdriver page 类
这个类的目的是为了简化page类的实例化,只需要定义public page成员变量 然后再 启动driver后 通过反射实例化page 后面可以直接点出page实例 package crazy.sel ...
- class_options
一.class_option :stylesheet, :type => :boolean, :default => "true", :description => ...
- Oracle数据安全(四)j角色管理
一.角色管理的概述 1.角色的概念 为了简化数据库权限的管理,在Oracle数据库中引入了角色的概念.所谓的角色就是一系列相关权限的集合. 2.角色的特点 在数据库中,角色的名称必须是唯一的,不能与用 ...
- JavaScript返回顶部特效
样式: <style type="text/css"> /*返回顶部特效*/ a { border: none; text-decoration: none; outl ...
- 【Java】Swing+IO流实现一个简单的文件加密程序(demo版)
留着参考 EncrytService package com.my.service; import java.io.File; import java.io.FileInputStream; impo ...
- 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
import random import string def GenKey(length): chars = string.ascii_letters + string.digits return ...
- 验证码插件代码:image.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- React Native导航器之react-navigation使用
在上一节Navigation组件,我们使用系统提供的导航组件做了一个跳转的例子,不过其实战能力不强,这里推荐一个超牛逼的第三方库:react-navigation.在讲react-navigation ...