题解 | #删除链表峰值#
删除链表峰值
https://www.nowcoder.com/practice/30a06e4e4aa549198d85deef1bab6d25
/* * function ListNode(x){ * this.val = x; * this.next = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ function deleteNodes( head ) { // write code here if(!head && head.next) return head; let l1 = head; let l2 = head.next while(l2.next) { if(l1.val < l2.val && l2.val > l2.next.val) { l1.next = l2.next; l2 = l2.next; } else { l1=l2 l2 = l2.next } } return head } module.exports = { deleteNodes : deleteNodes };
重新声明一个链表l1和l2 依次比较和移动即可