【leetcode】Remove Linked List Elements(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
思路:简单题。
- //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 fakeHead = ListNode(); //伪头部,化简代码
- fakeHead.next = head;
- ListNode *p = &fakeHead;
- while(NULL != p->next)
- {
- if(p->next->val == val) //当前数字的下一个需要被删除 删掉后重新判断当前位置的下一个数
- p->next = p->next->next;
- else //当前位置的下一个不需要删除,把当前位置后移
- p = p->next;
- }
- return fakeHead.next;
- }
- };
【leetcode】Remove Linked List Elements(easy)的更多相关文章
- 【leetcode】Reverse Linked List II (middle)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 【leetcode】Merge Two Sorted Lists(easy)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- 【leetcode】Contains Duplicate & Rectangle Area(easy)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】10.Regular Expression Matching(dp)
[题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...
- LeetCode 203. Remove Linked List Elements (移除链表中的项)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- 【leetcode】Swap Nodes in Pairs (middle)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【leetcode】Reverse Nodes in k-Group (hard)☆
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- 【leetcode】Search for a Range(middle)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
随机推荐
- ktouch移动端事件库
最近闲来无事,写了个移动端的事件库,代码贴在下面,大家勿拍. /** @version 1.0.0 @author gangli @deprecated 移动端触摸事件库 */ (function ( ...
- 去除tabbar的灰线
去掉导航栏的边界灰线 [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBar ...
- 如何在R中加载”xlsx”包
1.下载安装对应系统位数的JDK包(Java SE Development Kit) 2.完成后,安装rJava包-low-level r to Java Interface install.pack ...
- HDOJ 3709 Balanced Number
数位DP... Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java ...
- LUXURY 7
A.Little Pony and Expected Maximum Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:% ...
- Java Io 流(输入输出流)
IO流,也就是输入和输出流,可分为字节流和字符流. 1. 字节流 (1). InputStream 输入流,用于读取文件 输入流常用API: inputStream.read() 读取一个字节 in ...
- 通过NavMeshObstacle解决NavMesh防卡
http://www.unity蛮牛.com/thread-33383-1-1.html. 许久未曾发帖了,最近忙于换工作的问题,经常处于纠结状态,so...偶尔上蛮牛还能看到大家对我的支持,感觉还是 ...
- Opencv混合高斯模型前景分离
#include "stdio.h" #include "string.h" #include "iostream" #include &q ...
- caffe学习系列(7):Blob,layer,Net介绍
参考:http://www.cnblogs.com/denny402/p/5073427.html
- java中的jComBox的基本用法
jComBox获取选中的字符串 getSelectedItem().toString()返回一个字符串 getSelectedItem()返回一个对象. getSelectedIndex()得到选择值 ...