# include <bits/stdc++.h>
using namespace std;
struct list_node{
int val;
struct list_node * next;
};
list_node * input_list()
{
int val, n;
scanf("%d", &n);
list_node * phead = new list_node();
list_node * cur_pnode = phead;
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;
}
list_node * remove_value(list_node * head, int num)
{
//////在下面完成代码
while(head->val==num) head=head->next;
list_node* pre=head;
list_node* cur=head->next;
while(cur!=NULL){
if(cur->val==num) {
pre->next=cur->next;
cur=pre->next;
}else{
pre=pre->next;
cur=cur->next;
}
}
return head;
}
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 num;
scanf("%d", &num);
list_node * new_head = remove_value(head, num);
print_list(new_head);
return 0;
} import java.io.*;
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().trim());
String[] rawInput = br.readLine().trim().split(" ");
int num = Integer.parseInt(br.readLine().trim());
Node head = buildLinkedList(rawInput);
head = delNode(head, num);
printLinkedList(head);
br.close();
}
private static Node delNode(Node head, int num) {
if (null == head) {
return null;
}
Node curNode = head, preNode = null;
while (null != curNode) {
if (curNode.value == num) {
// 换头
if (curNode == head) {
head = head.next;
} else {
preNode.next = curNode.next;
}
} else {
preNode = curNode;
}
curNode = curNode.next;
}
return head;
}
private static Node buildLinkedList(String[] rawInput) {
Node head = null, curNode = null;
for (int i = 0; i < rawInput.length; i++) {
Node tmpNode = new Node(Integer.parseInt(rawInput[i]));
if (null == head) {
head = tmpNode;
} else {
curNode.next = tmpNode;
}
curNode = tmpNode;
}
return head;
}
private static void printLinkedList(Node head) {
if (null == head) {
return;
}
StringBuilder sb = new StringBuilder();
while (null != head) {
sb.append(head.value).append(" ");
head = head.next;
}
System.out.print(sb.toString().trim());
}
}
class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
this.next = null;
}
} #include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
int data;
struct LNode* next;
}LNode,*LinkList;
LinkList Init();
void GetNum(LinkList H,int n);
void Del(LinkList H,int e);
int main()
{
LinkList H,q;
int n,e;
H = Init();
scanf("%d",&n);
GetNum(H,n);
scanf("%d",&e);
Del(H,e);
q = H->next;
while(q){
printf("%d ",q->data);
q = q->next;
}
system("pause");
}
LinkList Init()
{
LinkList H = malloc(sizeof(LNode));
H->next = NULL;
return H;
}
void GetNum(LinkList H,int n)
{
LinkList p = H,q;
int e;
for(int i = 0; i < n; i++){
scanf("%d",&e);
q = (LinkList)malloc(sizeof(LNode));
q->data = e;
q->next = NULL;
p->next = q;
p = q;
}
}
void Del(LinkList H,int e)
{
LinkList p = H,q;
while(p->next){
if(p->next->data == e){
q = p->next;
p->next = q->next;
free(q);
}
p = p->next;
}
} 基本思路:
1、首先将所有节点值存在数组中,然后再根据循环放入链表
2、删除链表方法
3、打印
class ListNode{
int val;
ListNode next;
ListNode(int val){
this.val=val;
}
}
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
ListNode head=new ListNode(a[0]);
ListNode cur=head;
for(int i=1;i<n;i++){
cur.next=new ListNode(a[i]);
cur=cur.next;
}
cur.next=null;
int val=sc.nextInt();
ListNode node=removeElements(head,val);
printList(node);
}
public static void printList(ListNode head){
while(head!=null){
System.out.print(head.val+" ");
head=head.next;
}
}
public static ListNode removeElements(ListNode head,int val){
if(head==null){
return head;
}
ListNode cur=head;
ListNode res=null;
while(cur!=null){
if(cur.val==val){
if(cur==head){
head=head.next;
}else{
res.next=cur.next;
}
}else{
res=cur;
}
cur=cur.next;
}
return head;
}
}
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 Node removeValue(Node head, int num) {
while (head != null) {
if (head.value != num) {
break;
}
head = head.next;
}
Node pre = head;
Node cur = head;
while (cur != null) {
if (cur.value == num) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = pre.next;
}
return head;
}
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 newHead = removeValue(head, num);
printList(newHead);
}
} list_node * remove_value(list_node * head, int num)
{
//////在下面完成代码
struct list_node *pre,*cur;
while(head!=NULL&&head->val==num){
head=head->next;
free(cur);
cur=head;
}
pre=head;
cur=pre->next;
while(cur){
if(cur->val==num){
pre->next=cur->next;
free(cur);
cur=cur->next;
}else{
pre=cur;
cur=cur->next;
}
}
return head;
}