数据结构:单向链表。指针操作,注意Null Pointer的情况 以及链表头指针的操作

 

代码:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode cur = head;
while(head != null && head.val == val){
head = head.next;
cur = cur.next;
}

while(cur != null && cur.next != null){
if(cur.next.val == val) cur.next = cur.next.next;
else cur = cur.next;
}
return head;
}
}

posted on 2016-01-10 09:29  爱推理的骑士  阅读(78)  评论(0编辑  收藏  举报