删除链表的节点 (删除链表的节点)

 

思路:

// @Title: 删除链表的节点 (删除链表的节点)
// @Author: qisiii
// @Date: 2022-02-20 17:35:03
// @Runtime: 0 ms
// @Memory: 40.5 MB
// @comment: 
// @flag: 
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        if(head.val==val){return head.next;}
        ListNode pre=head;ListNode cur=head.next;
        while(cur!=null){
            if(cur.val==val){
                pre.next=cur.next;
                break;
            }
            pre=cur;
            cur=cur.next;
        }
        return head;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18