package com. my. util;
public class SingleNode {
public int value;
public SingleNode next;
public SingleNode ( int data) {
this . value = data;
}
}
package com. my. suanfa;
import java. util. Stack;
import com. my. util. SingleNode;
public class Solution11 {
public SingleNode removeValue1 ( SingleNode head, int num) {
Stack< SingleNode> stack = new Stack < SingleNode> ( ) ;
while ( head != null) {
if ( head. value != num) {
stack. push ( head) ;
}
head = head. next;
}
while ( ! stack. isEmpty ( ) ) {
stack. peek ( ) . next = head;
head = stack. pop ( ) ;
}
return head;
}
public SingleNode removeValue2 ( SingleNode head, int num) {
while ( head != null) {
if ( head. value != num) {
break ;
}
head = head. next;
}
SingleNode pre = head;
SingleNode cur = head;
while ( cur != null) {
if ( cur. value == num) {
pre. next = cur. next;
} else {
pre = cur;
}
cur = cur. next;
}
return head;
}
}