从无头单链表中删除节点及单链表的反转操作

问题描述:假设有一个没有头指针的单链表。一个指针指向此单链表中间的一个节点(不是第一个,也不是最后一个节点),请将该节点从单链表中删除。


一般链表的删除需要顺着头结点向下找到当前待删节点的前驱节点,然后让前驱节点指向后驱节点就行了。这里,没有头结点,就没办法找到前驱结点。但我们可以采用“狸猫换太子”的做法。我们把当前结点“看成”是前驱结点,把后续节点当做待删结点删除(删除之前,记下后续结点的值),只需要让当前结点指向后驱结点的后驱结点。最后把后续结点值赋给当前结点的值。

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<assert.h>  
  4.   
  5. typedef struct node{  
  6.     int data;  
  7.     node *next;  
  8. }Node;  
  9.   
  10. void printlist(Node *head_ptr);  
  11. void delete_random_node(Node *current);  
  12.   
  13. int main(){  
  14.     Node n1, n2, n3;  
  15.     n1.data = 10;  
  16.     n1.next = &n2;  
  17.     n2.data = 20;  
  18.     n2.next = &n3;  
  19.     n3.data = 30;  
  20.     n3.next = NULL;  
  21.     printf("Before deleting\n");  
  22.     printlist(&n1);  
  23.     delete_random_node(&n2);  
  24.     printf("\nAfter deleting\n");  
  25.     printlist(&n1);  
  26.     return 0;  
  27. }  
  28.   
  29. void printlist(Node *head_ptr){    
  30.     Node *ptr = head_ptr;   
  31.     while(ptr != NULL){    
  32.         printf("%d    ", ptr->data);    
  33.         ptr = ptr->next;    
  34.     }    
  35.     printf("\n");    
  36. }    
  37.   
  38. void delete_random_node(Node *current){  
  39.     assert(current != NULL);  
  40.     Node *next = current->next;  
  41.     if(next != NULL){  
  42.         current->next = next->next;  
  43.         current->data = next->data;  
  44.     }  
  45. }  
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef struct node{
	int data;
	node *next;
}Node;

void printlist(Node *head_ptr);
void delete_random_node(Node *current);

int main(){
	Node n1, n2, n3;
	n1.data = 10;
	n1.next = &n2;
	n2.data = 20;
	n2.next = &n3;
	n3.data = 30;
	n3.next = NULL;
	printf("Before deleting\n");
	printlist(&n1);
	delete_random_node(&n2);
	printf("\nAfter deleting\n");
	printlist(&n1);
	return 0;
}

void printlist(Node *head_ptr){  
    Node *ptr = head_ptr; 
    while(ptr != NULL){  
        printf("%d    ", ptr->data);  
        ptr = ptr->next;  
    }  
    printf("\n");  
}  

void delete_random_node(Node *current){
	assert(current != NULL);
	Node *next = current->next;
	if(next != NULL){
		current->next = next->next;
		current->data = next->data;
	}
}

扩展问题


编写一个函数,给定一个链表的头指针,要求只遍历一次,将单链表中的元素顺序反转过来。

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3.   
  4. typedef struct node{  
  5.     int data;  
  6.     node *next;  
  7. }Node;  
  8.   
  9. Node *reverse_linklist(Node *head);  
  10. void printlist(Node *ptr);  
  11.   
  12. int main(){  
  13.     Node n1, n2, n3;  
  14.     Node *head;  
  15.     head = (Node *)malloc(sizeof(Node));  
  16.     head->next = &n1;  
  17.     n1.data = 10;  
  18.     n1.next = &n2;  
  19.     n2.data = 20;  
  20.     n2.next = &n3;  
  21.     n3.data = 30;  
  22.     n3.next = NULL;  
  23.   
  24.     printf("Before reversing\n");  
  25.     printlist(head);  
  26.     printf("\nAftering reversing\n");  
  27.     printlist(reverse_linklist(head));  
  28.     return 0;  
  29. }  
  30.   
  31. void printlist(Node *head_ptr){  
  32.     Node *ptr = head_ptr->next;  
  33.     while(ptr != NULL){  
  34.         printf("%d    ", ptr->data);  
  35.         ptr = ptr->next;  
  36.     }  
  37.     printf("\n");  
  38. }  
  39.   
  40. Node *reverse_linklist(Node *head){  
  41.     Node *p = head->next;  
  42.     Node *e = NULL;  
  43.     Node *q;  
  44.     while(p->next != NULL){  
  45.         q = p->next;  
  46.         p->next = e;  
  47.         e = p;  
  48.         p = q;  
  49.     }  
  50.     p->next = e;  
  51.     head->next = p;  
  52.     return head;  
  53. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值