203. Remove Linked List Elements
Once cur jumps to cur.next, cur.val would be ignored. so we have to make sure there is no val in any cur's following nodes before setting cur = cur.next.
And again, always check node.next valid or not before using it. (while(cur.next and cur.next.val == val))
No need for two loops, just one loop with "while(cur.next)" is enough, and set cur.next = cur.next.next when cur.next.val == val. When the statement is satisfied, cur is cur.next = cur.next.next already; when the statement isn't satisfied, move cur = cur.next.
Last updated