leetcode203-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

思路:删掉链表中value=val的节点,因为链表中删除表首和其他位置的节点的操作是不同的,删除其他位置的节点,需要找到所删节点的前一个节点。

           在本题中,为原链表添加一个伪头节点。

            head 一步一步往前走,pre在head前面,如果head.val=val,则在pre中删除head


leetcode203-Remove Linked List Elements