您的位置:首页 > 其它

203. Remove Linked List Elements(链表)

2017-08-22 10:27 417 查看
https://leetcode.com/problems/remove-linked-list-elements/description/

题目:删除链表中的元素

思路:直接用2个指针即可。

/**
* 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* dummy = new ListNode(0);
dummy->next = head;
head = dummy;

while (head->next != NULL) {
if (head->next->val == val) {
head->next = head->next->next;
} else {
head = head->next;
}
}

return dummy->next;

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: