题解 | #牛群排列去重#
牛群排列去重
https://www.nowcoder.com/practice/8cabda340ac6461984ef9a1ad66915e4
/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
function deleteDuplicates( head ) {
// write code here
if(!head) return head
let pre = head
let cur = head.next
while(cur){
if(cur.val == pre.val){
pre.next = cur.next
cur = cur.next
}else{
pre = cur
cur = cur.next
}
}
return head
}
module.exports = {
deleteDuplicates : deleteDuplicates
};
考点:l 链表
思路: 保存前一个结点,让前一个结点与当前结点对比,如果相同就将当前的结点删除。pre和cur结点向后移动一位,直到当前结点为空,循环结束。
小天才公司福利 1199人发布
