逆置链表

逆置链表

1.创建新的链表,把要逆置的链表从最结尾把结点一个一个的取出来,尾插到新链表中,或者把要逆置的链表从头把结点一个一个的拿出来,头插到新链表中去。
2.用栈实现链表的逆置,链表的逆置就是要把在前面的链表放在后面,这跟栈先进先出的特点吻合

import java.util.Stack;

public class reverseList {
    static class Node {
        int val;
        Node next=null;
        Node(int val){
            this.val=val;
        }
    }
    //逆置链表
    public static Node reverseList(Node head){
        Stack<Node> stack=new Stack<>();

        Node cur=head;
        while (cur!=null){
            stack.push(cur);
            cur=cur.next;
        }
        Node newhead=null;
        Node last=null;
        while (!stack.isEmpty()){
            Node node=stack.pop();
            if(newhead==null){
                newhead=node;
                last=node;
            }else {
                last.next=node;
                last = node;
            }
        }
        last.next = null;//保证最后一个结点的下一个指向null
        return newhead;
    }

    public static void main(String[] args) {
        Node n1=new Node(1);
        Node n2=new Node(2);
        Node n3=new Node(3);
        Node n4=new Node(4);
        Node n5=new Node(5);
        n1.next=n2;
        n2.next=n3;
        n3.next=n4;
        n4.next=n5;
        print(n1);
        Node head= reverseList(n1);
        print(head);
    }

    private static void print(Node newhead) {
        Node cur=newhead;
        while (cur!=null){
            if(cur.next == null) {
                System.out.println(cur.val);
                cur = cur.next;
            }else {
                System.out.print(cur.val + "-->");
                cur = cur.next;
            }

        }
    }
}

3.原地逆转链表(不用新申请新的空间)
先定义三个引用,一个指向要遍历当前链表的的结点(pcur),一个指向当前结点的下一个(next),一个指向当前结点的前一个(prev)。先用next将pcur.next保存起来,以备最后更新pcur时使用,让pcur.next指向prev,更新pcur。
注:需注意,你要确定最后的想要的新的链表的头是被这三个引用中的哪一个指向的。

public class reverseLinklist {
    public static void main(String[] args) {
        Node head=new Node(1);
        Node n1=new Node(2);
        Node n2=new Node(3);
        Node n3=new Node(4);
        head.next=n1;
        n1.next=n2;
        n2.next=n3;
        Node newhead=reverse(head);
        Node cur=newhead;
        while (cur.next!=null){
            System.out.print(cur.val+"-->");
            cur=cur.next;
        }
        System.out.print(cur.val);

    }
    /*
    * 逆置链表
    * 原地逆置,不需要申请新的空间
    * */
    public static Node reverse(Node head){
        Node pcur=head;
        Node prev=null;
        Node next;
        if(head==null||head.next==null){
            return head;
        }
        while (pcur!=null){
            /*if(pcur.next==null){     //控制最后我们要的头结点是被pcur指向的
                next=pcur.next;        //不改变pcur的值直接返回
                pcur.next=prev;
                break;
            }*/
            next=pcur.next;
            pcur.next=prev;
            prev=pcur;
            pcur=next;   //如果没有前面的判断,pcur最后是指向空的
        }
        return prev;     //分清到低是哪个引用指向了新的,我们想要的头节点
    }
    static class  Node {
        int val;
        Node next=null;
        Node(int val){
            this.val=val;
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值