LeetCode-203. Remove Linked List Elements
2017-03-03 11:02
295 查看
问题:
https://leetcode.com/problems/remove-linked-list-elements/?tab=Description
Remove all elements from a linked list of integers that have value val.
删除值为val的元素。
Example: Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5
分析:
注意删除头结点的情况。
参考C++代码:
https://leetcode.com/problems/remove-linked-list-elements/?tab=Description
Remove all elements from a linked list of integers that have value val.
删除值为val的元素。
Example: Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5
分析:
注意删除头结点的情况。
参考C++代码:
/** * 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* pre=head; ListNode* cur=head; if(head==NULL) return head; while(cur!=NULL){ if(head->val==val){ head=head->next; cur=head; pre=head; continue; } if(cur->val==val){ pre->next=cur->next; cur=cur->next; continue; } pre=cur; cur=cur->next; } return head; } };
相关文章推荐
- leetcode unique path I&&II
- LeetCode Remove Element
- LeetCode——Remove Duplicates from Sorted Array
- [leetcode] Candy
- (leetcode )Unique Binary Search Trees
- [Leetcode]Clone Graph
- [C++]LeetCode: 132 Find Minimum in Rotated Sorted Array II (二分查找)
- [Leetcode] 125. Valid Palindrome
- Binary Tree Inorder Traversal ——LeetCode
- leetcode之Invert Binary Tree
- leetCode 98.Validate Binary Search Tree (有效二叉搜索树) 解题思路和方法
- leetcode - 位运算题目汇总(上)
- [LeetCode] Perfect Squares
- [LeetCode] 57. Insert Interval 解决思路
- LeetCode笔记:Move Zeros[Difficulty: Easy]
- leetcode——46——Permutations
- [leetcode] 【链表】 138. Copy List with Random Pointer
- 【一天一道LeetCode】#257. Binary Tree Paths
- [leetcode] 399. Evaluate Division 解题报告
- [LeetCode]Reverse Linked List II