删除一个链表第k个节点
原创
©著作权归作者所有:来自51CTO博客作者杉菜酱_的原创作品,请联系作者获取转载授权,否则将追究法律责任
删除一个链表第k个节点。
import java.util.Scanner;
public class KNode {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
ListNode head = new ListNode(sc.nextInt());
ListNode cur = head;
for (int i = 1; i < n; ++i) {
cur.next = new ListNode(sc.nextInt());
cur = cur.next;
}
ListNode help = new ListNode(-1);
help.next = head;
cur = help;
ListNode next = head;
for (int i = 0; i < m - 1; ++i) {
cur = cur.next;
}
cur.next = cur.next.next;
StringBuilder sb = new StringBuilder();
cur = help.next;
while (cur != null) {
sb.append(cur.val + "").append(" ");
cur = cur.next;
}
System.out.println(sb.toString());
}
}
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
}
}