Q16 LeetCode203 移除链表元素

1.注意头结点是否满足要求

2.注意移动时节点是否为空

 

复制代码
 1 class Solution {
 2     public ListNode removeElements(ListNode head, int val) {
 3         while(head!=null&&head.val==val){
 4             head=head.next;
 5         }
 6       ListNode cur=head;
 7       
 8       while (cur != null && cur.next != null) {
 9             if (cur.next.val == val) {
10                 cur.next = cur.next.next;
11             } else {
12                 cur = cur.next;
13             }
14         }
15       return head;
16     }
17 }
复制代码

 

posted @   清川1  阅读(2)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示