【LeetCode】28.Linked List— Remove Linked List Elements删除链表元素

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

Example:

Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

说到删除,首先想到定义两个指针,分别指向要被删除的结点和该结点的前驱结点。这里还需要考虑头结点是需要删除结点的特殊情况。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode*p=head;      //定义前驱结点指针
        if(p==NULL) return NULL;
        ListNode*q=p->next;   //定义要删除的结点指针
        while(1)
        {
            if(q==NULL){ //如果链表只有一个结点且头结点是要删除的结点
                if(head->val==val)
                {
                    delete p;
                    return NULL;
                }
                else return head;
            }
            else{      //如果链表不止一个结点
                if(head->val==val)  //头结点是要删除的结点
                {
                    head=q;
                    delete p;
                    p=head;
                    q=p->next;
                }
                else if(q->val==val)  //找到要删除的结点
                {
                    p->next=q->next;
                    delete q;
                    q=p->next;
                }
                else{          //没有要删除的结点,则一头后移。
                    p=p->next;
                    q=p->next;
                }
            }
        }
        return head;
    }
};

原文地址:https://www.cnblogs.com/hu-19941213/p/11429735.html

时间: 2024-08-12 19:17:38

【LeetCode】28.Linked List— 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

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 很简单吧,没什么好说的.直接上AC代码: ListNode* removeElements(ListNode* hea

【leetcode?python】 203. Remove Linked List Elements

# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = None class Solution(object):    def removeElements(self, head, val):        """        :type head: ListNode

leetcode第203题-Remove Linked List Elements

题目要求:从一个链表中删除指定的元素. 分析:首先要考虑链表的非空问题,我想这应该是判断链表和数组问题最首先要考虑处理的问题.其次是如果前几个元素都是与指定元素相同,也要做一次处理,使head指向(在非空的情况下)与指定元素不相等的第一个元素.注意:在这里我碰到了一个很可笑的问题,就是while里面循环的判断条件,应该让head!=NULL在前面,这样就能保证head->val的值的问题,一直在这里出问题,后来才检查出来.然后再定义两个指针,第一个指针p指向当前的元素,第二个指针指向下一个节点,

[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 容易题:Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点

题目: 在O(1)时间复杂度删除链表节点 给定一个单链表中的表头和一个等待被删除的节点(非表头或表尾).请在在O(1)时间复杂度删除该链表节点.并在删除该节点后,返回表头. 样例 给定 1->2->3->4,和节点 3,返回 1->2->4. 解题: 方法好贱,先把要删除节点后面一个节点的值赋给删除节点,这样需要删除节点就删除了,再把删除节点指向删除节点后面节点的节点 就像这样: node.val = node.next.val; node.next = node.next.

19. Remove Nth Node From End of List 倒数删除链表元素

思路:第一时间想到的肯定是通过添加计数器解决这个问题,但是问题产生了,链表具有单向性,统计总数,还是计算当前数目都在效率上面很吃亏. 借用快慢指针的思想.可以安排两个同速指针,但是他们存在先后顺序,就可以解决这个问题. public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { if(head == null || n <= 0) return null; ListNode dummy = ne

【leetcode 简单】 第六十九题 删除链表中的节点

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 4 -> 5 -> 1 -> 9 示例 1: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9. 示例 2: 输入: head = [4,5,1,9], node = 1 输

LeetCode 第 19 题 (Remove Nth Node From End of List)

LeetCode 第 19 题 (Remove Nth Node From End of List) Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the li