Remove Nth Node From End of List
来源:互联网 发布:情义我心知 结局 编辑:程序博客网 时间:2024/04/30 15:44
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) { // Start typing your C/C++ solution below // DO NOT write int main() function ListNode* p = head; ListNode* q = head; while (p) { if (n != -1) { --n; } else { q = q->next; } p = p->next; } if (n == 0) { head = head->next; delete q; } else if (n == -1) { p = q->next; if (p != NULL) { q->next = p->next; delete p; } } return head; }};
- 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
- Linux内核中位图的使用
- Java框架集合-Set(HashSet)
- CBmpProgCtrl漂亮的进度条
- asmack相关
- 预编译 ASP.NET Web 服务
- Remove Nth Node From End of List
- Linux mdelay与msleep的区别以及中断中不能休眠的原因
- Proxy 代理模式
- c++模板类(一)理解编译器的编译模板过程
- Computer Based Social Engineering Tools: Social Engineer Toolkit (SET)
- 正则表达式基础知识
- VS2010编译的程序在XP上无运行库执行,以及ADO在XP上报80004003的错误处理
- Java 开发 2.0: 使用 Hibernate Shards 进行切分
- 【Linux基础】Linux文件