题解 | #牛群排列去重#
牛群排列去重
https://www.nowcoder.com/practice/8cabda340ac6461984ef9a1ad66915e4
- 题目考察的知识点:链表的删除
- 题目解答方法的文字分析:遍历并用双指针判断相等值
- 本题解析所用的编程语言:js
/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
function deleteDuplicates( head ) {
// write code here
const set=new Set();
const dummyHead = new ListNode(0);
dummyHead.next = head;
let l= dummyHead;
// 链表长度 <= 1
if (head == null || head.next == null) {
return head;
}
let r = l.next;
while (r != null) {
if (l.next.val == r.val) {
// 重复节点,删除
l.next.next = r.next;
} else {
// r 非重复节点,l.next向后移动
l.next = r;
}
// r向后移动
r = r.next;
}
return head;
}
module.exports = {
deleteDuplicates : deleteDuplicates
};
查看14道真题和解析
小天才公司福利 1211人发布