Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

203. Remove Linked List Elements #8

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

依然链表,新建头,while循环遍历链表 public class Solution { public ListNode removeElements(ListNode head, int val) { if(head == null) return head; ListNode subs = new ListNode(0); subs.next = head; ListNode flag = subs; while(flag.next != null) { if(flag.next.val == val) { flag.next = flag.next.next; } else { flag = flag.next; } } return subs.next; } }