Leetcode 203. Remove Linked List Elements (Easy) (cpp)
2016-07-12 12:49
441 查看
Leetcode 203. Remove Linked List Elements (Easy) (cpp)
Tag: Linked List
Difficulty: Easy
Tag: Linked List
Difficulty: Easy
/* 203. 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) { if (head == NULL) { return head; } ListNode dummy(-1); ListNode *prev = &dummy; do { if (head -> val == val) { head = head -> next; } else { prev -> next = new ListNode(head -> val); prev = prev -> next; head = head -> next; } }while (head != NULL); return dummy -> next; } };
相关文章推荐
- C语言程序的存储区域
- Leetcode 141. Linked List Cycle (Easy) (cpp)
- C++打包DLL在C#中调用
- Leetcode 21. Merge Two Sorted Lists (Easy) (cpp)
- Leetcode 290. Word Pattern (Easy) (cpp)
- Leetcode 299. Bulls and Cows (Easy) (cpp)
- C++中的引用
- 【BZOJ1977】【BJOI2011】严格次小生成树
- Cpp环境【BZOJ1626】【Usaco2007 Dec】【Vijos 1693】 修建道路
- Leetcode 242. Valid Anagram (Easy) (cpp)
- Leetcode 350. Intersection of Two Arrays II (Easy) (cpp)
- Leetcode 349. Intersection of Two Arrays (Easy) (cpp)
- Leetcode 36. Valid Sudoku (Easy) (cpp)
- Leetcode 204. Count Primes (Easy) (cpp)
- Leetcode 66. Plus One (Easy) (cpp)
- Leetcode 121. Best Time to Buy and Sell Stock (Easy) (cpp)
- Leetcode 169. Majority Element (Easy) (cpp)
- C++中的auto_ptr智能指针的作用及使用方法详解
- Leetcode 283. Move Zeroes (Easy) (cpp)
- Leetcode 81. Search in Rotated Sorted Array II (Medium) (cpp)