lamiastella

remove from linked list

Mar 26th, 2016
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. public class Solution {
  2.     /**
  3.      * @param head a ListNode
  4.      * @param val an integer
  5.      * @return a ListNode
  6.      */
  7.     public ListNode removeElements(ListNode head, int val) {
  8.         ListNode tmp = new ListNode(0);
  9.  
  10.         while (head!=null){
  11.             if (head.val == val) {
  12.                 head = head.next;
  13.             }
  14.             else break;
  15.         }
  16.         tmp.next = head;
  17.         while (tmp!=null && tmp.next != null ) {
  18.                 if (tmp.next.val == val) {
  19.                     tmp.next = tmp.next.next;
  20.                     tmp=tmp.next;
  21.                     continue;
  22.                 }
  23.             tmp=tmp.next;
  24.         }
  25.         if (tmp != null) {
  26.  
  27.             if (tmp.val == val) {
  28.                 tmp = tmp.next;
  29.             }
  30.         }
  31.         return head;
  32.        
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment