Leetcode每日一题:203.remove-linked-list-elements(移除链表元素)
思路:先利用一个while 将head指向第一个不为val的结点,随后对head->next进行判断,若值为val,则用一个循环跳过之间所有连续的值为val的结点,然后继续对head->next进行判断;
/*** 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)
{if (head == NULL) //判断是不是空链表return head;while (head && head->val == val) //使hh和head指向第一个值不为val的结点,这肯定是结果链表的起始节点{head = head->next;}ListNode *hh = head;while (head){if (head->next == NULL) break;if (head->next->val == val){ListNode *pre = head;head = head->next;while (head && head->val == val){head = head->next;}pre->next = head;}else{head = head->next;}}return hh;
}
};
Leetcode每日一题:203.remove-linked-list-elements(移除链表元素)相关推荐
- leetcode python3 简单题203. Remove Linked List Elements
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零三题 (1)题目 英文: Remove all elements from ...
- leetcode 203. Remove Linked List Elements(链表)
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- 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 ...
- leetcode 203 Remove Linked List Elements
Remove all elements from a linked list of integers that have valueval. Example Given: 1 --> 2 ...
- LeetCode 203. Remove Linked List Elements
题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 ...
- [leetcode]203. Remove Linked List Elements链表中删除节点
这道题很基础也很重要 重点就是设置超前节点 public ListNode removeElements(ListNode head, int val) {//超前节点ListNode pre = n ...
- Leetcode每日一题:24.swap-nodes-in-pairs(两两交换链表中的节点)
思路:递归或者迭代,每次使用三个节点,pre.now.next即可实现交换,主要对链表头两个节点的交换有特殊性(不能用pre): 奇怪的是递归和迭代所占空间竟然差不多: struct ListNode ...
- leetcode每日刷题计划-简单篇day8
leetcode每日刷题计划-简单篇day8 今天是纠结要不要新买手机的一天QAQ想了想还是算了吧,等自己赚钱买,加油 Num 70 爬楼梯 Climbing Stairs class Solutio ...
- leetcode每日一题--前缀树;前缀哈希;深搜;面试题 08.04. 幂集;648. 单词替换面试题 01.09. 字符串轮转;剑指 Offer II 062. 实现前缀树
leetcode每日一题 ps:今天的每日一题没意思,简单的模拟,自己换一道 面试题 08.04. 幂集 幂集.编写一种方法,返回某集合的所有子集.集合中不包含重复的元素. 说明:解集不能包含重复的子 ...
- Leetcode每日一题:171.excel-sheet-column-number(Excel表列序号)
思路:就是168题的反命题,进制的方式完美解决: Leetcode每日一题:168.excel-sheet-column-title(Excel表名称) class Solution {public: ...
最新文章
- 机器学习训练中常见的问题和挑战!
- Linux之LVM与磁盘配额(quota)详解
- php动态生成apk渠道包,Android自动生成渠道包
- bzoj1002 生成树计数 找规律
- springboot2自定义HttpTraceRepository
- DataV 支持 token 验证啦!
- leetcode738. 单调递增的数字
- Linux基金会亚太区与开源中国达成战略合作 共同推动中国开源人才培养
- PSD分层模板素材丨只需掌握三大规则!电商海报秒脱俗套
- ASP .NET CORE MVC 部署Windows 系统上 IIS具体步骤---.Net Core 部署到 IIS位系统中的步骤...
- Python中logging日志使用
- gbcbig.shx字体的BUG
- 华为交换机初始化和配置SSH和TELNET远程登录方法
- 鸿基酒店应收应付报表生成系统
- mysql mpm_mysql监控工具:zabbix+MPM(Performance Monitor for MySQL)
- java计算机毕业设计bs架构实习管理系统MyBatis+系统+LW文档+源码+调试部署
- 一些简单的css,html,js笔记分享给大家,希望能够帮助到大家
- 星起航:抖音小店如何提升店铺复购率
- 等式约束优化(可行点)
- python中的format什么意思中文-python中format函数什么意思