【LeetCode刷题-链表】--203.移除链表元素
203.移除链表元素
方法:定义一个节点指向头节点head,避免头结点单独操作
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode removeElements(ListNode head, int val) {ListNode dumyhead = new ListNode(0);dumyhead.next = head;ListNode cur = dumyhead;while(cur.next != null){if(cur.next.val == val){cur.next = cur.next.next;}else{cur = cur.next;}}return dumyhead.next;}
}