题解 | #删除链表峰值#
删除链表峰值
https://www.nowcoder.com/practice/30a06e4e4aa549198d85deef1bab6d25
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteNodes (ListNode head) { // write code here if (head == null) return head; ListNode ret = new ListNode(0); ret.next = head; while (head.next != null && head.next.next != null) { if (head.val < head.next.val && head.next.val > head.next.next.val) { head.next = head.next.next; } head = head.next; } return ret.next; } }
感觉题目有点含糊,删除前后值比当前值大的节点,没有说只删一个呀,那所有符合的节点都应该被删除吧。
比如{2,3,4,5,1}中除了首尾不能删,{3,4,5}都是符合要求的。