void remove_node_wired(list_node * node) { //因为无法获取前驱,将后继的值赋给当前节点,删除后继结点 //如果是尾节点 if(node->next==NULL) return; //中间节点 else{ node->val=node->next->val; node->next=node->next->next; } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class ListNode { public int val; public ListNode next = null; public ListNode(int val) { this.val = val; this.next = null; } } public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] elements = br.readLine().trim().split(" "); int target = Integer.parseInt(br.readLine()); ListNode head = new ListNode(Integer.parseInt(elements[0])); ListNode cur = head; ListNode targetNode = target == 0? head: null; for(int i = 1; i < n; i++){ cur.next = new ListNode(Integer.parseInt(elements[i])); cur = cur.next; if(i + 1 == target) targetNode = cur; } deleteNode(targetNode); while(head != null) { System.out.print(head.val + " "); head = head.next; } } private static void deleteNode(ListNode targetNode){ if(targetNode == null) return; targetNode.val = targetNode.next.val; targetNode.next = targetNode.next.next; } }
# include <bits/stdc++.h> using namespace std; struct list_node{ int val; struct list_node * next; }; list_node * find_kth_node(list_node * head, int k) { list_node * c = head; for (int i = 1; i < k; ++i) c = c->next; return c; } list_node * input_list(void) { int n, val; list_node * phead = new list_node(); list_node * cur_pnode = phead; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &val); if (i == 1) { cur_pnode->val = val; cur_pnode->next = NULL; } else { list_node * new_pnode = new list_node(); new_pnode->val = val; new_pnode->next = NULL; cur_pnode->next = new_pnode; cur_pnode = new_pnode; } } return phead; } void remove_node_wired(list_node * node) { //////在下面完成代码 /* 将当前节点的值更换为下一个节点的值,然后删除下一个节点 */ list_node* cur=node->next; node->val=cur->val; node->next=cur->next; } void print_list(list_node * head) { while (head != NULL) { printf("%d ", head->val); head = head->next; } puts(""); } int main () { list_node * head = input_list(); int n; scanf("%d", &n); list_node * node = find_kth_node(head, n); remove_node_wired(node); print_list(head); return 0; }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static class Node { public int value; public Node next; public Node(int value) { this.value = value; } } public static Node listGenerator(int length, String[] numbers) { Node head = new Node(Integer.parseInt(numbers[0])); Node cur = head; for (int i = 1; i < length; i++) { cur.next = new Node(Integer.parseInt(numbers[i])); cur = cur.next; } cur.next = null; return head; } public static void printList(Node head) { while (head != null) { System.out.print(head.value +" "); head = head.next; } System.out.println(); } public static void remove(Node node) { if (node == null) { return; } else if (node.next == null) { throw new RuntimeException("cannot remove last node!"); } else { node.value = node.next.value; node.next = node.next.next; } } public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bufferedReader.readLine()); String[] numbers = bufferedReader.readLine().split(" "); int num = Integer.parseInt(bufferedReader.readLine()); Node head = listGenerator(n, numbers); Node node = head; int index = 1; while (node != null && index != num) { node = node.next; index++; } remove(node); printList(head); } }