lintcode 刪除鏈表中的元素
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
/*
* @param head: a ListNode
* @param val: An integer
* @return: a ListNode
*/
public ListNode removeElements(ListNode head, int val) {
ListNode first=head;
if(first==null){
return null;
}
while(first.val==val){
first=first.next;
if(first==null){
return null;
}
}
ListNode a=first;
ListNode b=a.next;
while(b!=null){
if(b.val==val){
a.next=b.next;
}else{
a=a.next;
}
b=b.next;
}
return first;
}
}
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
/*
* @param head: a ListNode
* @param val: An integer
* @return: a ListNode
*/
public ListNode removeElements(ListNode head, int val) {
ListNode first=head;
if(first==null){
return null;
}
while(first.val==val){
first=first.next;
if(first==null){
return null;
}
}
ListNode a=first;
ListNode b=a.next;
while(b!=null){
if(b.val==val){
a.next=b.next;
}else{
a=a.next;
}
b=b.next;
}
return first;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.