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

本文共 1065 字,大约阅读时间需要 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; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode *removeNthFromEnd(ListNode *head, int n) {        if(head==NULL||head->next==NULL)return NULL;        ListNode *former=head;        ListNode *later=head;        ListNode *temp=NULL;        int count=0;        while(former->next)        {            count++;            former=former->next;            if(count>=n)            {                temp=later;                later=later->next;            }        }        if(temp==NULL)head=later->next;        else temp->next=later->next;        delete later;        return head;    }};

 

转载于:https://www.cnblogs.com/Vae1990Silence/p/4283669.html

你可能感兴趣的文章
(转,记录用)jQuery页面加载初始化的3种方法
查看>>
C++常量的引用 const
查看>>
51nod 1101 换零钱 【完全背包变形/无限件可取】
查看>>
python单例设计模式(待补充)
查看>>
Binary Tree Inorder Traversal
查看>>
HDU 1394 Minimum Inversion Number (数据结构-线段树)
查看>>
ansible-playbook && Roles && include
查看>>
String s String s=null和String s="a"区别
查看>>
[Alpha阶段]第二次Scrum Meeting
查看>>
关于Java 8 forEach
查看>>
.NET设计模式(1):1.1 单例模式(Singleton Pattern)
查看>>
创建模态对话框和非模态对话框
查看>>
08-图8 How Long Does It Take
查看>>
二维数组中最大连通子数组
查看>>
java 正则表达式-忽略大小写与多行匹配
查看>>
mac 上亚马逊密钥登录
查看>>
css选择器中:first-child与:first-of-type的区别
查看>>
nopcommerce 二次开发
查看>>
NHibernate入门实例
查看>>
IBM_DS5020磁盘阵列做raid、热备并把盘阵挂在服务器上的步骤
查看>>