链表反转

链表反转

方法一:逐步插入
在这里插入图片描述
首先把头节点和后面节点断开,逐个遍历节点,并把遍历的节点插入到头节点之后。

C代码:

#include<stdio.h> 
typedef struct Node{
	int data;
	struct Node *next;
}Node;
//初始化链表
void initTail(int array[10],int n,Node **head){
	*head = (Node*)malloc(sizeof(Node));
	(*head)->next = NULL;
	Node *tail = *head;
	Node *temp;
	int i;
	for(i=0;i<n;i++){
		temp = (Node*)malloc(sizeof(Node));
		temp->data= array[i];
		tail ->next = temp;
		tail = tail->next;
	}
	tail -> next = NULL;
}
//遍历链表
void foreach(Node *head){
	Node *h = head;
	while(h->next!=NULL){
		printf("%d ",h->next->data);
		h = h->next;	
	}
	printf("\n");
}
void reverse(Node *head){
	Node *h = head;
	Node *link = head->next;
	h->next = NULL;
	Node *p;
	while(link!=NULL){
		p = link;
		link = link->next;
		p->next=head->next;
		head->next=p;
	}
}
int main(){
		Node *head = NULL;
	int array[5]={
		1,2,3,4,5
	};
	initTail(array,5,&head);
	printf("逆序前:");
	foreach(head);
	reverse(head);
	printf("\n逆序后:"); 
	foreach(head);
}

输出:
在这里插入图片描述

java



public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] array = {1,2,3,4,5};
		Node head = initLink(array);
		System.out.print("逆序前:");
		foreach(head);
		reserve(head);
		System.out.println();
		System.out.print("逆序后:");
		foreach(head);
	}
	public static void reserve(Node head) {
		Node node = head.next;
		head.next = null;//头与链表先断开连接
		Node temp = null;
		while(node!=null) {
			temp = node;
			node = node.next;
			temp.next = head.next;
			head.next = temp;
		}
		
	}
	//初始化链表
	public static Node initLink(int[] array) {
		Node head = new Node();
		Node tail = head;
		Node temp = null;
		for(int i=0;i<array.length;i++) {
			temp = 	new Node() ;
			temp.data=array[i];
			tail.next=temp;	
			tail = tail.next;
		}
		tail.next = null;
		return head;
	}
	public static void foreach(Node head) {
		Node t = head;
		while(t.next!=null) {
			System.out.print(t.next.data+" ");
			t = t.next;
		}
	}
}
class Node{
	public int data;
	public Node next;
}

结果:
在这里插入图片描述

方法二:递归逆转

可以把递归函数当成一种状态,如下图所示
在这里插入图片描述
链表递归过程:
在这里插入图片描述
断开环:
在这里插入图片描述
代码如下:
C.

#include<stdio.h> 
typedef struct Node{
	int data;
	struct Node *next;
}Node;
void initTail(int array[10],int n,Node **head){
	*head = (Node*)malloc(sizeof(Node));
	(*head)->next = NULL;
	Node *tail = *head;
	Node *temp;
	int i;
	for(i=0;i<n;i++){
		temp = (Node*)malloc(sizeof(Node));
		temp->data= array[i];
		tail ->next = temp;
		tail = tail->next;
	}
	tail -> next = NULL;
}
void foreach(Node *head){
	Node *h = head;
	while(h->next!=NULL){
		printf("%d ",h->next->data);
		h = h->next;	
	}
	printf("\n");
}

void reverseHelper(Node *head,Node* node){
	if(node->next==NULL){//递归结束条件,递归到链表尾部时把头节点指向尾部
		head->next = node;
		return ;
	}
	reverseHelper(head,node->next);
	node->next->next = node;	
}
void reverse(Node *head){
	Node *p = head->next;
	reverseHelper(head,head->next);
	p->next = NULL;//递归结束需要解环
}

int main(){
	Node *head = NULL;
	int array[5]={
		1,2,3,4,5
	};
	initTail(array,5,&head);
	printf("逆序前:");
	foreach(head);
	reverse(head);
	printf("逆序后:");
	foreach(head);
	return 0;
}


结果:
在这里插入图片描述

java.

package com;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] array = { 1, 2, 3, 4, 5 };
		Node head = initLink(array);
		System.out.print("逆序前:");
		foreach(head);
		reserve(head);
		System.out.println();
		System.out.print("逆序后:");
		foreach(head);
	}

	public static void reserve(Node head) {
		Node p = head.next;
		reserveHelper(head,head.next);
		p.next = null;
	}

	public static void reserveHelper(Node head,Node node) {
		if(node.next == null) {
			head.next = node;
			return;
		}
		reserveHelper(head,node.next);
		node.next.next = node;
	}

	// 初始化链表
	public static Node initLink(int[] array) {
		Node head = new Node();
		Node tail = head;
		Node temp = null;
		for (int i = 0; i < array.length; i++) {
			temp = new Node();
			temp.data = array[i];
			tail.next = temp;
			tail = tail.next;
		}
		tail.next = null;
		return head;
	}

	public static void foreach(Node head) {
		Node t = head;
		while (t.next != null) {
			System.out.print(t.next.data + " ");
			t = t.next;
		}
	}
}

class Node {
	public int data;
	public Node next;
}

结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值