Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- /**
- * @param head a ListNode
- * @param val an integer
- * @return a ListNode
- */
- public ListNode removeElements(ListNode head, int val) {
- ListNode tmp = new ListNode(0);
- while (head!=null){
- if (head.val == val) {
- head = head.next;
- }
- else break;
- }
- tmp.next = head;
- while (tmp!=null && tmp.next != null ) {
- if (tmp.next.val == val) {
- tmp.next = tmp.next.next;
- tmp=tmp.next;
- continue;
- }
- tmp=tmp.next;
- }
- if (tmp != null) {
- if (tmp.val == val) {
- tmp = tmp.next;
- }
- }
- return head;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment