[LeetCode-19] Remove Nth Node From End of List(删除倒数第N个节点)
来源:互联网 发布:虚拟专用网络 编辑:程序博客网 时间:2024/05/16 12:45
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.
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { if(!head) return NULL; if(0 == n) { return head; } struct ListNode *p = head; struct ListNode *free_node = NULL; int ListNodeLen = 0; /*1.链表长度计算*/ while(p) { ListNodeLen ++; p = p->next; } if(n > ListNodeLen) return head; p = head;// printf("[%d] ListNodeLen: %d\n",__LINE__,ListNodeLen); /*2.第一个节点*/ if(n == ListNodeLen) { /*第一个节点*/ head = p->next; free(p); // printf("[%d] ListNodeLen: %d\n",__LINE__,ListNodeLen); return head; }// printf("[%d] ListNodeLen: %d\n",__LINE__,ListNodeLen); int count = 0; /*3.定位到倒数第n+1个位置,删除第n个节点*/ while(1) { if(count == (ListNodeLen-n-1)) { /*定位到倒数第n+1个位置*/ free_node = p->next; p->next = p->next->next; free(free_node); break; } p = p->next; count++; } return head;}
0 0
- [LeetCode-19] Remove Nth Node From End of List(删除倒数第N个节点)
- [C++]LeetCode 19: Remove Nth Node From End of List(删除链表中倒数第n个节点)
- LeetCode(Remove Nth Node From End of List)删除链表倒数第n个节点
- 【LeetCode】Remove Nth Node From End of List 删除链表中倒数第n个节点- Easy +
- LeetCode 19 Remove Nth Node From End of List 移除倒数第N个节点
- leetcode:Remove Nth Node From End of List(删除链表倒数第n个节点)【面试算法题】
- #19 Remove Nth Node From End of List(删除链表倒数第N个节点)
- Remove Nth Node From End of List 删除链表倒数第N个节点
- LintCode Remove Nth Node From End of List 删除链表中倒数第n个节点
- [LeetCode]—Remove Nth Node From End of List 删除链表的倒数第n个节点
- leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法
- leetcode-19. Remove Nth Node From End of List(删除链表倒数第N个节点)
- Remove Nth Node From End of List 链表删除倒数第N个元素@LeetCode
- LeetCode Remove Nth Node From End of List 删除倒数第n个元素
- Leetcode Remove Nth Node From End of List 删除链表倒数第n个元素
- 174.Remove Nth Node From End of List-删除链表中倒数第n个节点(容易题)
- LeetCode--Remove Nth Node From End of List(移除链表的倒数第N个节点)Python
- LeetCode[19]Remove Nth Node From End of List 删除链表倒数第n个元素
- centos7装linux翻译软件
- CEOI 1999 Parity game (并查集+离散化)
- LeetCode|Same Tree-java
- 成为程序员前需要做的10件事
- SELINUX 导致rsync无法同步文件
- [LeetCode-19] Remove Nth Node From End of List(删除倒数第N个节点)
- Maven学习笔记(四)——MyEclipse导入、创建&配置Maven工程
- jeecms各种标签类(大部分,并没有包含一些其他的如text_cut html_cut之类)
- LDAP概念和原理
- RobotFramework安装和配置说明
- C中结构体的存储分配
- java.util.concurrent包中线程安全的集合简介
- 8个让程序员追悔莫及的职业建议
- Java实现字符串的反转