博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
19. Remove Nth Node From End of List
阅读量:4671 次
发布时间:2019-06-09

本文共 911 字,大约阅读时间需要 3 分钟。

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.

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {    struct ListNode* p1 = head;    struct ListNode* p2 = head;        for(int i = 0; i < n; i++){        p1 = p1->next;    }    if(!p1){ //special condition: delete head        head = head->next;        free(p2);        return head;    }        while(p1->next){        p1 = p1->next;        p2 = p2->next;    }        p1 = p2->next;    p2->next = p2->next->next;    free(p1);    return head;}

 

转载于:https://www.cnblogs.com/qionglouyuyu/p/5400712.html

你可能感兴趣的文章
20145101 《Java程序设计》第7周学习总结
查看>>
P2678 跳石头
查看>>
Alpha阶段项目复审
查看>>
ArrayQueue详解(待解决)
查看>>
ASP.NET 安全认证(四)
查看>>
IE9+下如何让input的placeholder样式生效
查看>>
使用 web storage 制作简单留言本
查看>>
61组第二次团队作业
查看>>
利用jsonp实现跨域请求
查看>>
查看Oracle表中的指定记录在数据文件中的位置
查看>>
ServicePointManager.ServerCertificateValidationCallback 冲突的解决
查看>>
Notes on UNPv1 Ch.5
查看>>
Dubbo实战快速入门 (转)
查看>>
旅游电车
查看>>
Win32中文件的操作
查看>>
【分治】动态点分治 ([ZJOI2007]捉迷藏)
查看>>
「一入 Java 深似海 」系列课程
查看>>
技术胖Flutter第四季-19导航父子页面的跳转返回
查看>>
阶段1 语言基础+高级_1-3-Java语言高级_04-集合_03 斗地主案例(单列)_1_斗地主案例的需求分析...
查看>>
Unity 3D 正交相机(Orthographic)
查看>>