LeetCode | Remove Nth Node From End of List(移除链表中倒数第n个结点)
来源:互联网 发布:ubuntu 17.04 软件源 编辑:程序博客网 时间:2024/05/22 02:22
题目:
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.
题目解析:
这道题目和【剑指offer】面试题15:链表中倒数第k个结点很相像。只是这里要删除结点,要考虑的情况更多一点。
虽然这道题目比较容易,但是有很多地方要注意:输入的是空指针怎么办?输入的是0怎么处理?(为0的时候,n-1不是-1,而是最大的正数,因为变量设为unsigned)当链表的个数正好是n个的时候怎么处理?那么就要删除第一个结点,头指针的值会改变。
基于以上的情况分析,才能写出可靠的代码
LinkList RemoveNth(LinkList *head,unsigned int n){ if(n == 0) return *head; if(*head == NULL){ printf("list is NULL\n"); return NULL; } LinkList p = *head; for(int i = 0;i < n-1;i++){ p = p->next; if(!p){ printf("lenth < n\n"); exit(1); } } if(p->next == NULL){ //证明长度刚好是n p = *head; *head = (*head)->next; free(p); return *head; } p = p->next; LinkList q = *head; //此时的q指向的是倒数第n个结点的父节点! while(p->next){ p = p->next; q = q->next; } p = q->next; //p重新赋值为q的子节点 q->next = p->next; free(p); return *head;}
完整的程序代码:
#include <stdio.h>#include <stdlib.h>typedef struct LNode{ int data; struct LNode *next;}ListNode,*LinkList;void CreateList(LinkList *head,int n){ for(int i = 0;i < n;++i){ LinkList p = (LinkList)malloc(sizeof(ListNode)); if(p == NULL){ printf("malloc error\n"); exit(1); } scanf("%d",&(p->data)); p->next = *head; *head = p; }}void DestoryList(LinkList *head){ if(*head == NULL) return ; LinkList p = *head; LinkList q = p->next; while(q){ free(p); p = q; q = p->next; } free(p); *head = NULL;}void PrintList(LinkList head){ LinkList p = head; while(p){ printf("%d ",p->data); p = p->next; } printf("\n");}LinkList RemoveNth(LinkList *head,unsigned int n){ if(n == 0) return *head; if(*head == NULL){ printf("list is NULL\n"); return NULL; } LinkList p = *head; for(int i = 0;i < n-1;i++){ p = p->next; if(!p){ printf("lenth < n\n"); exit(1); } } if(p->next == NULL){ //证明长度刚好是n p = *head; *head = (*head)->next; free(p); return *head; } p = p->next; LinkList q = *head; //此时的q指向的是倒数第n个结点的父节点! while(p->next){ p = p->next; q = q->next; } p = q->next; //p重新赋值为q的子节点 q->next = p->next; free(p); return *head;}int main(){ LinkList head = NULL; int n; while(scanf("%d",&n) != EOF){ CreateList(&head,n); PrintList(head); printf("input the delete num:"); scanf("%d",&n); RemoveNth(&head,n); PrintList(head); DestoryList(&head); } return 0;}
0 0
- LeetCode | Remove Nth Node From End of List(移除链表中倒数第n个结点)
- LeetCode 19 Remove Nth Node From End of List 移除倒数第N个节点
- 19.Remove Nth Node From End of List(移除单链表中倒数第N个结点)
- LeetCode--Remove Nth Node From End of List(移除链表的倒数第N个节点)Python
- leetcode 3. 移除链表的倒数第n个节点 Remove Nth Node From End of List
- 19. Remove Nth Node From End of List (移除链表倒数第n个节点)
- 19. 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个节点)
- 删除链表的倒数第n个结点 Remove Nth Node From End of List
- Remove Nth Node From End of List 删除链表的倒数第n个结点
- 【Leecode】207Remove 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个结点
- 【LeetCode-面试算法经典-Java实现】【019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)】
- Remove Nth Node From End of List "移除链表中的倒数第N项"
- [C++]LeetCode 19: Remove Nth Node From End of List(删除链表中倒数第n个节点)
- Leetcode #19. Remove Nth Node From End of List 移除倒数第N个链表节点 解题小节
- 单字节,多字节的自动类型识别方法
- smartFloat
- ubuntu安装rpm的方法
- CRC32、MD5、SHA1算法校验基本概念
- 基础—重载和覆盖区分
- LeetCode | Remove Nth Node From End of List(移除链表中倒数第n个结点)
- 线段树区间维护(各种操作)hdu2871
- v4l2 review
- 分销
- Java语言的运行机制
- Excel VBA 学习总结 - 数据验证与正则表达式
- 开源日志系统比较
- 调用Android隐藏类(系统aidl文件)
- 黑马程序员——C语言之循环结构