Remove Nth Node From End of List
来源:互联网 发布:网络被攻击 编辑:程序博客网 时间:2024/06/05 01:51
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.
/** * 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;}if(n<0){return head;}//n是合理的 ListNode* p1=head;ListNode* p2=head;for(int i=n-1;i>0;--i){ p2=p2->next;}ListNode* pPre=NULL; //引入这个变量,因为12-2的错误while(p2->next){ pPre=p1; p2=p2->next; p1=p1->next;}if(pPre==NULL){head=p1->next;delete p1;}else{ListNode* pDelete=pPre->next;pPre->next=pDelete->next;delete pDelete;} return head; }};
0 0
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node from End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- spark实现hive的合并输入很多小文件为指定大小的大文件的优化功能
- std::numeric_limits<float>::max() warning C4003: “max”宏的实参不足
- 概率论--条件概率与乘法公式
- hdu4352 XHXJ's LIS 数位DP+状态压缩
- Mongodb学习笔记五、查询操作
- Remove Nth Node From End of List
- 黑马程序员——java基础学习笔记——第五天
- 黑马程序员——java基础——异常
- 基于 spring4 java config(配置)的servlet3 文件上传于下载
- 欢迎使用CSDN-markdown编辑器
- Valid Parentheses
- Rotate Array
- JStorm之Topology调度
- 一个球从100米高的自由落下,每次落地后反跳回原高度的一半,再落下,再反弹。求第 10次落地时,共经过多少米,第10次反弹多高。