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 –> 4 –> 5 –> 6, val = 6

返回: 1 –> 2 –> 3 –> 4 –> 5

分析:

首先,既然是移除链表的元素,有2种移法,一种是在遍历到某个元素时,去比较它的下一个元素(移除它的下一个元素),直接修改next指针;另一种是遍历到某个元素时,将下一个元素的值和next指针赋值给当前元素(变相删除了当前元素),但是这种做法有一个局限,就是无法删除链表尾部的元素。

所以,我们只能采用第一种做法,但是这种做法又隐含了一个问题,就是先要确认第一个元素是与val不等的(不然如果从head开始遍历,也就是去比较head的下一个元素,这个时候其实是跳过了head.val与val的比较)。

下面的方案中,我们先通过一个循环来找出满足不等val的条件的第一个元素,将head指针指向它。这步操作完成后,如果head不为空的话,再采用刚刚所说的第一种做法,也就是始终比较它的下一个元素,直至下一个元素为空。

Java版代码(时间复杂度O(n)):

/**
 * 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) {
        while(head!=null&&head.val==val){
            head=head.next;
        }
        if(head==null){
            return null;
        }
        ListNode current=head;
        while(current.next!=null){
            if(current.next.val==val){
                current.next=current.next.next;
            }else{
                current=current.next;
            }
        }
        return head;
    }
}
时间: 2024-08-09 22:01:26

Leet Code OJ 203. Remove Linked List Elements [Difficulty: Easy]的相关文章

Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 翻译: 给定一个下标k,返回第k行的杨辉三角. 例如给定k=3,返回[1,3,3,1]. 提示:你可以优化你的算法,让它只使用O(k)的额

Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]

题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 翻译: 合并2个已经排序的链表,并且返回一个新的链表.这个新的链表应该由前面提到的2个链表的节点所组成. 分析: 注意头节点的处理,和链表结束(next为null)的处理.以下代码新增了一个头指针,来把头节点

Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]

题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be

Leet Code OJ 58. Length of Last Word [Difficulty: Easy]

题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]

题目: Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 翻译: 给定一个正数,返回它类似Excle中对应的列标题. 分析: 关联问题:"Excel Sheet Column Number"

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 题目大意: 删除链表中全部的目标元素. 代码如下:

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 ==

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 1 /** 2 * Definition for singly-linked list. 3 * struct Lis

Java [Leetcode 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 解题思路: 链表操作. 代码如下: /** * Definition for singly-linked