lintcode 删除链表中的元素
2018-01-22 15:50
225 查看
/**
* 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;
}
}
相关文章推荐
- LintCode-删除链表中的元素
- [LintCode]112.删除排序链表中的重复元素
- LintCode 删除链表中的元素
- LintCode入门练习——452. 删除链表中的元素
- Lintcode 入门-452. 删除链表中的元素
- lintcode ----删除排序链表中的重复元素
- LintCode 452 删除链表中的元素
- Lintcode 452.删除链表中的元素
- [lintcode]Remove Linked List Element 删除链表中的元素
- [LintCode]删除链表中的元素
- [LintCode]452.删除链表中的元素
- 【LintCode 入门】452. 删除链表中的元素
- 【Lintcode】删除链表中的元素
- [Lintcode] #452 删除链表中的元素
- 删除链表中的元素-LintCode
- 一、lintcode刷题记录--矩阵面积、二叉树的最大节点、整数排序、删除链表中的元素
- LintCode 452.删除链表中的元素 C++
- [lintcode]-链表 删除链表中的元素
- LintCode-删除排序链表中的重复元素
- [Lintcode]删除链表中元素