刪除鏈表中等於給定值 val 的全部節點。spa
示例:code
輸入: 1->2->6->3->4->5->6, val = 6 輸出: 1->2->3->4->5
解法:沒什麼好說的,直接刪便可
class Solution { public ListNode removeElements(ListNode head, int val) { while(head!=null && head.val == val){ head = head.next; } if(head == null){ return head; } ListNode pre = head; ListNode cur = head.next; while(cur!=null){ if(cur.val == val){ pre.next = cur.next; cur.next = null; cur = pre; }else{ pre = pre.next; } cur = cur.next; } return head; } }