LeetCode---Remove Nth Node From End of List
来源:互联网 发布:威纶通触摸屏编程手册 编辑:程序博客网 时间:2024/06/04 01:26
题目大意:给出一个链表,和一个数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=head; vector<ListNode*> Node; while(p!=NULL){ Node.push_back(p); p=p->next; } int num=Node.size(); if(n==num) { ListNode* temp=head; head=head->next; delete temp; } else if(n==1) { ListNode *pre=Node[num-n-1]; pre->next=NULL; delete Node[num-n]; } else{ ListNode *pre=Node[num-n-1]; pre->next=Node[num-n+1]; delete Node[num-n]; } return head; }};
0 0
- LeetCode[Linked List]: Remove Nth Node From End of List
- 19. Remove Nth Node From End of List leetcode list
- [Leetcode]LeetCode 19:Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- [Leetcode] Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- leetcode 40: Remove Nth Node From End of List
- [LeetCode] Remove Nth Node From End of List
- Leetcode 19 Remove Nth Node From End of List
- [Leetcode] Remove Nth Node From End of List
- [LeetCode]Remove Nth Node From End of List
- LeetCode-Remove Nth Node From End of List
- [LeetCode] Remove Nth Node From End of List
- LeetCode 19: Remove Nth Node From End of List
- LeetCode - Remove Nth Node From End of List
- LeetCode | Remove Nth Node From End of List
- leetcode之Remove Nth Node From End of List
- Spring MVC 全注解的方式实现单个和多个文件上传
- performSelector多参数
- Contains Duplicate II
- LA4015 Caves
- markdown字体字号背景色
- LeetCode---Remove Nth Node From End of List
- 杨辉三角python实现
- Apple Watch如何进行真机调试及问题汇总
- Linux Epoll介绍和程序实例
- 严蔚敏数据结构习题3.31
- 开启JBoss as 7的access_log功能
- BZOJ 1050: [HAOI2006]旅行comf
- hdu1466计算直线的交点数&2050 折线分割平面【DP】
- iOS9的 NSURLSession