19. Remove Nth Node From End of List 链表遍历
来源:互联网 发布:淘宝屋加盟 编辑:程序博客网 时间:2024/05/16 14:50
题目地址
设置两个指针,一个先走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, *q = head, *pre; if (head == NULL || head->next == NULL) return NULL; while (n--) p = p->next; if (p == NULL) { pre = head; head = head->next; delete pre; return head; } while (p->next != NULL) { p = p->next; q = q->next; } pre = q->next; q->next = pre->next; delete pre; return head; }};
0 0
- 19. 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
- WebGoat使用指南
- Fragment+RadioButton实现点击切换页面效果
- Android BroadcastReceiver
- FFT 模板
- 在IT技术博客上回顾一个文字冒险游戏。。。
- 19. Remove Nth Node From End of List 链表遍历
- Java并发之Semaphore的源码分析
- 【滤波算法】【C语言】(二)标量Kalman滤波的过程分析和证明及C实现
- 科学家进餐问题
- object c 归档和解档,其实就是java中的序列化和反序列化
- 221. Maximal Square
- HDU1040
- 文章标题
- Maven实战学习笔记、7.生命周期和插件