LeetCode (Remove Nth Node From End of List)
来源:互联网 发布:w3school node 编辑:程序博客网 时间:2024/06/05 14:55
Problem:
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) { int length = 0; ListNode l(0), *p = &l; l.next = head; while(head != NULL){ length++; head = head->next; } for (int i = 1; i <= length; i++){ if (i !=(length - n + 1)) p = p->next; else p->next = p->next->next; } return l.next; }};
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
- 采蘑菇
- AS 更新项目gradle方法
- 日期插件改为单选
- for循环嵌套输出数据
- BZOJ P1011[HNOI2008]遥远的行星
- LeetCode (Remove Nth Node From End of List)
- 关于C/C++数据结构中结构体的定义
- bzoj 1211: [HNOI2004]树的计数 (prufer序列+组合数学)
- RTSP 协议
- EasyDarwin开源流媒体云平台之EasyRMS录播服务器功能设计
- How to define alias in Linux
- eclipse正则表达式搜索
- MySQL原子性数据
- shell脚本中echo显示内容带颜色的实现方法