lintcode-easy-Remove Linked List Elements

Remove all elements from a linked list of integers that have value val.

Example

Given 1->2->3->3->4->5->3, val = 3, you should return the list as1->2->4->5

看起来这题不难,但是还是有些地方需要注意,据说面试要做到bug-free

p = p.next;执行之后,p有可能是null,所以在循环条件中要检查p是否为null。

其实任何时候访问一个对象的成员之前,都要注意检查这个对象是否为null,否则会出现异常。

/**
 * 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) {
        // Write your code here
        if(head == null)
            return head;

        ListNode fakehead = new ListNode(0);
        fakehead.next = head;

        ListNode p = fakehead;

        while(p!= null && p.next != null){
            while(p.next != null && p.next.val == val){
                p.next = p.next.next;
            }

            p = p.next;
        }

        return fakehead.next;
    }
}
时间: 2024-11-09 17:55:53

lintcode-easy-Remove Linked List Elements的相关文章

LeetCode Remove Linked List Elements 删除链表元素

题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 1

leetCode 203. Remove Linked List Elements 链表

203. Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 题目大意: 删除链表中全部的目标元素. 代码如下:

[LeetCode][JavaScript]Remove Linked List Elements

Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 https://leetcode.com/problems/r

leetcode_203题——Remove Linked List Elements(链表)

Remove Linked List Elements Total Accepted: 8053 Total Submissions: 29898My Submissions Question Solution Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val =

leetcode Remove Linked List Elements 203

Remove Linked List Elements Total Accepted: 11291 Total Submissions: 42824 Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3

203. Remove Linked List Elements - LeetCode

Question 203.?Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 Java实现: public ListNode removeElements(ListNode head, int val) { ListNode cur = head; while (cur != null) { if (cur.next != null && cur.next.val ==

[Linked List]Remove Linked List Elements

Total Accepted: 43183 Total Submissions: 160460 Difficulty: Easy Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --&

[LintCode] Remove Linked List Elements 移除链表元素

Remove all elements from a linked list of integers that have value val. Have you met this question in a real interview? Example Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

203. Remove Linked List Elements [easy] (Python)

题目链接 https://leetcode.com/problems/remove-linked-list-elements/ 题目原文 Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –>

Leet Code OJ 203. Remove Linked List Elements [Difficulty: Easy]

题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5 翻译: 从一个整数链表中移除所有value为val的元素. 例如 给定: 1 –> 2 –> 6 –> 3