package com.camnter.basicexercises.linklist;
import com.camnter.basicexercises.core.Node;
import java.util.Stack;
/**
* 链表删除特定值
*
* 使用栈去解,是特定值,不入栈
* 出栈的时候,来一趟反转链表
*
* @author CaMnter
*/
public class DeleteSpecifiedValue> {
public Node deleteSpecifiedValue(Node head, T target) {
if (head == null) return null;
Stack> stack = new Stack>();
while (head != null) {
if (head.value.compareTo(target) != 0) {
stack.push(head);
}
head = head.next;
}
/**
* 来一趟反转链表
*/
Node pre = null;
Node cur;
while (!stack.isEmpty()) {
cur = stack.pop();
cur.next = pre;
pre = cur;
}
return pre;
}
public static void main(String[] args) {
DeleteSpecifiedValue deleteSpecifiedValue = new DeleteSpecifiedValue();
Node.printLinkList(deleteSpecifiedValue.deleteSpecifiedValue(Node.getPalindromeLinkList(), 3));
}
}