import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @描述:
* @思路:
* @复杂度: 链表长度为N,要删除倒数第K个节点;最重要就是定位到它的前一个节点,即:第N-K个节点;
* 过程分两步:
* 1. 先从头到位遍历一遍遍历表,每遍历一个节点将K值减1, 将K值更新为K-N;
* 2. 再遍历一遍链表,每遍历一个节点将K值加1,直到K值为0停止,这样就将K值更新为0-(K-N)= N-K,此时的节点便是第N-k个节点,即:要删除"倒数第k个节点"的前一个节点。
* @链接:
*/
class RemoveLastKthNode {
public static Node removeLastKthNode(Node head, int k) {
Node cur = head;
while (cur != null) {
cur = cur.getNext();
k--;
}
if(k > 0 ) { // 说明不存在倒数第k个节点
System.out.println("说明不存在倒数第k个节点");
return head;
} else if(k == 0) { //说明头节点就是倒数第k个节点
return head.getNext();
} else {
cur = head;
while (++k != 0 ) {
cur = cur.getNext();
}
cur.setNext(cur.getNext().getNext());
return head;
}
}
}
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String[] s1 = input.readLine().split(" ");
String[] s2 = input.readLine().split(" ");
int n = Integer.parseInt(s1[0]);
int k = Integer.parseInt(s1[1]);
Node head = Node.createNodeList(s2);
head = RemoveLastKthNode.removeLastKthNode(head, k);
Node.printNodeList(head);
}
}
class Node {
private Node next;
private int value;
public Node(int value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public static Node createNodeList(Integer[] values) {
Node head = new Node((values[0]));
Node node = head;
for (int i = 1; i < values.length; i++) {
Node newNode = new Node(values[i]);
node.next = newNode;
node = newNode;
}
return head;
}
public static Node createNodeList(String[] values) {
Node head = new Node(Integer.parseInt(values[0]));
Node node = head;
for (int i = 1; i < values.length; i++) {
Node newNode = new Node(Integer.parseInt(values[i]));
node.next = newNode;
node = newNode;
}
return head;
}
public static void printNodeList(Node head) {
StringBuilder sb = new StringBuilder();
while (head != null) {
sb.append(head.getValue()).append(" ");
head = head.getNext();
}
System.out.println(sb.toString());
}
}