链表反转
方法一:逐步插入
首先把头节点和后面节点断开,逐个遍历节点,并把遍历的节点插入到头节点之后。
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;
}
结果: