【leetcode】【单链表】【19】Remove Nth Node From End of List
来源:互联网 发布:淘宝缠论指标知道 编辑:程序博客网 时间:2024/06/04 15:29
#include<iostream>using namespace std;struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};class Solution {public:ListNode* removeNthFromEnd(ListNode* head, int n) {int numOfNode = 0;ListNode* cur = head;while (cur){ //统计节点个数++numOfNode;cur = cur->next;}int beforeDelete = numOfNode - n;ListNode* temp = NULL;if (beforeDelete == 0){ //删除头结点情况temp = head;head = head->next;}else{ //删除非头结点情况cur = head;while (--beforeDelete > 0)cur = cur->next;temp = cur->next;cur->next = temp->next;}delete temp;return head;}ListNode* createList(ListNode* head){int numOfNode;int value;cout << "please input number of listNode:";cin >> numOfNode;cin >> value;head = new ListNode(value);ListNode* cur = head;for (int i = 1; i < numOfNode; ++i){cin >> value;ListNode* temp = new ListNode(value);cur->next = temp;cur = temp;}return head;}void printNode(ListNode* head){ListNode* cur = head;while (cur){cout << cur->val << " ";cur = cur->next;}cout << endl;}};int main(){ListNode* head = NULL;Solution lst;head = lst.createList(head);lst.printNode(head);head = lst.removeNthFromEnd(head, 2);lst.printNode(head);system("pause");return 0;}
0 0
- 【leetcode】【单链表】【19】Remove Nth Node From End of List
- [Leetcode]LeetCode 19:Remove Nth Node From End of List
- Leetcode 19 Remove Nth Node From End of List
- LeetCode 19: Remove Nth Node From End of List
- [Leetcode 19, Easy] Remove Nth Node From End of List
- LeetCode(19) Remove Nth Node From End of List
- [LeetCode 19] Remove Nth Node From End of List Solutions
- leetcode 19 Remove Nth Node From End of List
- Leetcode【19】:Remove Nth Node From End of List
- [leetcode 19] Remove Nth Node From End of List
- [Leetcode] 19 - Remove Nth Node From End of List
- LeetCode | #19 Remove Nth Node From End of List
- leetcode.19----------Remove Nth Node From End of List
- [Leetcode 19, Easy] Remove Nth Node From End of List
- LeetCode 19 Remove Nth Node From End of List
- leetcode-19 Remove Nth Node From End of List
- leetCode #19 Remove Nth Node From End of List
- leetcode 19 Remove Nth Node From End of List
- leetcode难度及面试频率
- Java 学习 类的继承
- linux spi framework
- 解决Cannot change version of project facet Dynamic web module to 2.5
- html5游戏开发设计技术解析Egret篇
- 【leetcode】【单链表】【19】Remove Nth Node From End of List
- java时间格式转换大全
- Aerospike C客户端手册———日志
- Java enum的用法详解
- Android: netd中DnsProxyListener的简单介绍
- 文件标签分割效果
- 安卓开发的各类存储
- btrace调试利器
- JS生成二维码