【leetcode】19. Remove Nth Node From End of List
来源:互联网 发布:当代大学生创业数据 编辑:程序博客网 时间:2024/06/12 00:48
/** * 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. */#include <iostream>#include <string>#include <vector>using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };ListNode* createList(ListNode*head, int num){ ListNode* pl = head; ListNode* node = new ListNode(num); if (pl == NULL) { head = node; } else { while (pl->next != NULL) { pl = pl->next; } pl->next = node; } return head;}ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode *ph, *pre, *back, *tmp; ph = pre = back = tmp = head; for (int i = 1; i < n; i++) { ph = ph->next; } while (ph->next != NULL) { pre = back; back = back->next; ph = ph->next; } tmp = pre->next; pre->next = back->next; free(tmp); return head;}//leetcode 利用二级指针删除ListNode* removeNthFromEnd1(ListNode* head, int n){ ListNode *ph, *tmp; ph = tmp = head; ListNode **back = &head; for (int i = 1; i < n; i++) { ph = ph->next; } while (ph->next != NULL) { back = &((*back)->next); ph = ph->next; } tmp = *back; *back = (*back)->next; free(tmp); return head;}int main(){ int arr[] = { 1, 2, 3, 4, 5 }; ListNode*head = NULL; for (int i = 0; i < 5; i++) { head = createList(head, arr[i]); } removeNthFromEnd1(head, 2); system("pause"); return 0;}
0 0
- 19. Remove Nth Node From End of List leetcode list
- [LeetCode]19.Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List Leetcode Python
- 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 OJ> 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List LeetCode
- leetcode 19. Remove Nth Node From End of List
- [LeetCode OJ] 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(JAVA)
- 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
- K-means聚类算法原理简单介绍
- Selector中的各种状态详解
- 机器学习: 最小二乘与贝叶斯估计
- OpenCV彩色图像转灰度图
- Java集合
- 【leetcode】19. Remove Nth Node From End of List
- oracle官方提到被频繁询问的问题2(rcp和或者rsh还需要么)
- 基于阿里云存储的UEditor(UEditor-for-aliyun-OSS)集成配置
- Kairosdb集群的搭建
- 我眼中的Android架构
- c/c++中全局变量
- 编码芯片EV1527射频学习
- Java枚举类型你真的知道么?
- 常见中文字体英文名称以及windows默认字体列表