题解 | #删除链表峰值#
删除链表峰值
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 || head.next==null){ return head; } //创建临时结点 ListNode t1=head; ListNode t2 =head.next; //循环遍历结点 while(t2.next!=null){ //删除条件判断 if(t1.val<t2.val && t2.val>t2.next.val){ //删除结点 t1.next = t2.next; }else{ //更新结点 t1 = t2; } //更新结点 t2 = t2.next; } return head; } }
面试高频TOP202 文章被收录于专栏
面试高频TOP202题解