前提是:有序链表
112233可以
123123这种就不可以
public class DeleteNodeSame {
public static void main(String[] args) {
Node node6 = new Node(3,null);
Node node5 = new Node(3,node6);
Node node4 = new Node(2,node5);
Node node3 = new Node(2,node4);
Node node2 = new Node(1,node3);
Node node1 = new Node(1,node2);
Node t = deleteSame(node1);
while (t!=null){
System.out.println(t.data);
t = t.nextNode;
}
}
public static Node deleteSame(Node node){
if(node == null || node.nextNode == null){
return node;
}
Node currNode = node;
while (currNode!=null){
Node temp = currNode.nextNode;
while (temp != null && currNode.data == temp.data) {
temp = temp.nextNode;
}
currNode.nextNode = temp;
currNode = currNode.nextNode;
}
return node;
}
static class Node{
public int data;
public Node nextNode;
public Node(int data,Node node){
this.data = data;
this.nextNode = node;
}
}
}