• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

Remove Nth From From End Of List

我的解法

第一次提交

介绍

别看我,看我干啥?看代码啊!

思路

构建链表:新列表节点(0,arr)

创建两个指针: x_pointer(遍历链表),y_pointer(遍历链表-删除节点)

代码

/*

* @Author: fox

* @ date : 2022-05-03 07:11:41

* @ LastEditors: fox

* @ lastedittime : 2022-05-03 08:2733:38

* @ description : https://leet代码。com/problems/remove-n-node-from-end-of-list/

*/

类别列表节点{

构造函数(瓦尔,下一个){

this.val=(val===未定义?0 : val);

this.next=(next===未定义?接下来空:

}

}

/**

* @描述:运行时: 90.90%内存使用量: 83.27%

* @param {ListNode} head链表头结点

* @param {number} n从链表末尾倒数的第几个节点

* @return {ListNode}

*/

const removeNthFromEnd=(head,n)={

const listNode=new ListNode(0,head);//构建链表

设x _ pointer=listNode

设y _ pointer=listNode

while (x_pointer.next) {

x _ pointer=x _ pointer .下一个

if (n=0) { //关键:通过不断减去差值来判断是否移动y指针指针

y _ pointer=y _ pointer .下一个

}否则{

n-;

}

}

y指针。next=y _ pointer。下一个。接下来;

返回listNode.next

};

让head=[1,2,3,4,5]

控制台。log(removeNthFromEnd(head,2)) //[1,2,3,5]

head=[1]

控制台。log(removeNthFromEnd(head,1)) //[]

head=[1,2]

控制台。log(removeNthFromEnd(head,1)) //[1]

Link to comment
Share on other sites