Remove Nth Node From End of List
来源:互联网 发布:ios 提取数组相同元素 编辑:程序博客网 时间:2024/06/05 22:56
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.
solution: two pointers, 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 || head->next == NULL){ return NULL; } ListNode *a = head; ListNode *b = head; for(int i = 0 ; i < n; i++){ b = b->next; } if(b==NULL){ return a->next; } while(b->next != NULL){ a = a->next; b = b->next; } //del a->next. ListNode *c = a->next->next; a->next = c; 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
- Java学习集锦
- Python多线程
- Android 开源框架Universal-Image-Loader 基本介绍及使用
- JAVA人学习ios swift(笔记)
- iOS --- 通过CPU实现的简单滤镜效果
- Remove Nth Node From End of List
- 美图秀秀专利---偏色检测算法研究
- exe文件打开方式错误 图标全部显示错误
- iOS中,在类的源文件(.m)中,@interface部分的作用?
- 转:了解iPhone应用程序中App目录结构
- 黑马程序员——Java之多线程
- 连不上EC2上部署的服务时的检查步骤
- 谈谈扁平化管理
- CF574B根据m简化时间