leetcode_019Remove Nth Node From End of List
来源:互联网 发布:阿里云电子发票 编辑:程序博客网 时间:2024/06/05 12:46
移除单链表倒数第N个节点。
测试平台是没有头结点的单链表!!!我最开始写的是带头结点的,所以测试不通过。
ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode* temp = head;int i = 0;while (temp != NULL){++i;temp = temp->next;}if (i != n){temp = head;int time = i - n - 1;int j = 0;while (j != time){temp = temp->next;++j;}if (temp != NULL)temp->next = temp->next->next;return head;}else return head->next; }
我个人觉得没有头结点的单链表各种操作都不怎么方便。我们在做插入、删除时可以为它创建一个头结点,最后再返回头结点的next就可以了。
0 0
- leetcode_019Remove 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
- leetcode 224: Basic Calculator
- strlen 转义字符
- 软考记录集(一)
- 除法优化
- 第八章 线性时间排序
- leetcode_019Remove Nth Node From End of List
- hdu 5424 Rikka with Graph II 哈密顿通路
- C 语言中32个关键字中几个特殊关键字
- js生成随机编号
- 如何搭建自定义php环境
- [c] poj1012 约瑟夫环
- winform,C#,打开文件对话框的使用
- pat(B)1007. 素数对猜想(素数打表)
- Note For Linux By Jes(17)-软件安装: RPM, SRPM 与 YUM 功能