链表----链表反转I

一、链表结点类

package com.hnust.Reversal;

public class ListNode {

    public int value;//结点的值
    public ListNode next;//下一个结点
    public ListNode(int val){
        super();
        this.value = val;
    }
    public ListNode(int value, ListNode next) {
        super();
        this.value = value;
        this.next = next;
    }
    @Override
    public String toString() {
        return "ListNode [value=" + value + "]";
    }

}

二、反转链表

我们可以通过把链表中链接节点的指针反转过来,从而改变链表的方向,以下两种算法是其实现。还有一种方法就是通过 栈 来实现:从头到尾遍历链表,依次将链表元素压入栈中,然后依次弹栈,从而实现链表的反转。

1.非递归算法的思路:

1)三个指针变量:
    pre:前驱结点
    p:当前结点
    next:下一个结点
2)关键的四个步骤:
    next = p.next;//将当前结点的下一个结点,用next存起来
    p.next = pre;//将当前结点的next指针指向前驱结点pre(反转单个结点)
    pre = p;//把p作为前驱结点(即 将pre结点往后移动一位)
    p =next;//next作为当前结点(即将p结点往后移动一位)
3)反转图示:

2.递归算法的思路

1)三个指针变量:
    p:当前结点
    next:当前结点的下一个结点
    tatil:尾部结点
2)
    递归处理next
    next.next=p;
    返回尾部结点
3)反转图示:

三、代码实现

package com.hnust.Reversal;

import org.junit.Test;
/**
 * 反转链表
 * @author Administrator
 *
 */
public class Reversal {

    //打印链表
    public void printList(ListNode head){
        ListNode p = head;
        while(p!=null){
            System.out.print(p.value+" ");
            p = p.next;
        }
        System.out.println();
    }

    //将数组转换成链表
    public ListNode arrayToList(int[] array){
        ListNode head = new ListNode(0);
        ListNode p = head;
        for(int value : array){
            p.next = new ListNode(value);
            p = p.next;
        }
        return head.next;
    }

    /**
     * 反转链表(非递归算法)
     * */
    public ListNode reverseList(ListNode head){
        if(head==null || head.next == null){
            return head;
        }else{
            ListNode pre = head;//定义前驱结点,初始化为head
            ListNode p = head.next;//定义当前结点,初始化为头结点的下一个结点
            ListNode next = null;//用来存放下一个结点,初始化为null
            while(p!=null){//遍历链表
                next = p.next;//将当前结点的下一个结点,用next存起来
                p.next = pre;//将当前结点的next指针指向前驱结点pre(反转单个结点)
                pre = p;//把p作为前驱结点(即 将pre结点往后移动一位)
                p =next;//把next作为当前结点(即 将p结点往后移动    一位)
            }
            head.next = null;//循环结束之后,去掉链表环
            //返回前驱结点pre,作为新的头结点(因为while循环结束的条件是p==null,所以根据最后一次的循环得出 最后只有
            //pre是指向最后一个结点的,所以将pre作为新的头结点)
            return pre;
        }
    }

    /**
     * 递归方法,时间复杂度和空间复杂度都是O(n)
     */
    public ListNode recursive(ListNode p){
        if(p.next==null){
            return p;
        }else{
            ListNode next = p.next;//取得p的下一个结点next
            ListNode tail = recursive(next);//递归处理next
            next.next = p;//反转单个结点 
            return tail;//把尾部结点作为新的头结点返回
        }
    }
    public ListNode reverseListRecursive(ListNode head){
        if(head == null || head.next == null){
            return head;
        }else{
            ListNode tail = recursive(head);
            head.next = null;//去掉头结点的链表环
            return tail;//把尾部结点作为新的头结点返回
        }

    }
    @Test
    public void test01(){
        int[] array={1,2,3,4,5,6};
        ListNode head = arrayToList(array);
        printList(head);
        ListNode newHead =reverseList(head);
        printList(newHead);
    }
    @Test
    public void test02(){
        int[] array={1,2,3,4,5,6};
        ListNode head = arrayToList(array);
        printList(head);
        ListNode newHead =reverseListRecursive(head);
        printList(newHead);
    }
}
通过栈实现链表的反转
Stack<ListNode> stack = new Stack<ListNode>();
    public ListNode reverse(ListNode head) {
        ListNode p = head;
        while (p != null) {
            //从头到尾遍历链表,将元素依次压入栈中
            stack.push(p);
            p = p.next;
        }
        ListNode newHead = null;    //新的头结点
        if (!stack.empty()) {
            newHead = stack.pop();
        }
        ListNode p1 = newHead;
        while (!stack.empty()) {
            p1.next = stack.pop();
            p1 = p1.next;
        }
        return newHead;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值