zoukankan      html  css  js  c++  java
  • 【LeetCode】203. 移除链表元素

    【题目描述】

    203. 移除链表元素

    删除链表中等于给定值 val 的所有节点。

    示例:

    输入: 1->2->6->3->4->5->6, val = 6
    输出: 1->2->3->4->5

    【提交代码】

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     struct ListNode *next;
     6  * };
     7  */
     8 
     9 
    10 struct ListNode* removeElements(struct ListNode* head, int val){
    11     struct ListNode *dummy;
    12     struct ListNode *cur;
    13     struct ListNode *p;
    14 
    15     dummy = (struct ListNode *)malloc( sizeof(struct ListNode ) );
    16     dummy->next = head;
    17 
    18     p = dummy;
    19     while( p->next != NULL )
    20     {
    21         cur = p->next;
    22         if( cur->val == val )
    23         {
    24             p->next = cur->next;
    25         }
    26         else
    27         {
    28             p = p->next;
    29         }
    30     }
    31 
    32     return dummy->next;
    33 }

    【解题思路】

    注:建立一个指向头节点的哑节点,保持操作的一致性,避免对删除头节点时的特殊判断;

  • 相关阅读:
    oracle-PL/SQL1
    ROS之Gazebo
    ROS之urdf 2
    ROS之urdf 1
    ROS 面部识别
    ROS x Arduino
    STM32F0的低功耗模式
    项目进度
    C++函数返回为引用
    STM32F0的多路ADC 无DMA
  • 原文地址:https://www.cnblogs.com/utank/p/13224813.html
Copyright © 2011-2022 走看看