剑指offer.18 单双指针删除链表的某个节点(java)

给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。

返回删除后的链表的头节点。

示例 1:

输入: head = [4,5,1,9], val = 5

输出: [4,1,9]

解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.

单指针解法:

class Solution {
    public ListNode deleteNode(ListNode head, int val) {
       if(head==null)
       return null;
       if(head.val==val)
       return head.next;
       ListNode cur=head;
       while(cur.next!=null&&cur.next.val!=val){
           cur=cur.next;
       }
           if(cur.next!=null)
           cur.next=cur.next.next;

           return head;
      
    }
}

双指针解法:

class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        if(head.val == val) return head.next;
        ListNode pre = head, cur = head.next;
        while(cur != null && cur.val != val) {
            pre = cur;
            cur = cur.next;
        }
        if(cur != null) pre.next = cur.next;
        return head;
    }
}


猜你喜欢

转载自blog.csdn.net/qq_44624536/article/details/115213107