反转链表~

一:初始化

public class ListNode {

    public int val;
    public ListNode next;

    public ListNode(int val,ListNode next){
        this.val = val;
        this.next = next;
    }

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder(64);
        sb.append("[");
        ListNode p = this;
        while (p!=null){
            sb.append(p.val);
            if (p.next!=null){
                sb.append(",");
            }
            p = p.next;
        }
        sb.append("]");
        return sb.toString();
    }

}

方法一:

public ListNode reverseList(ListNode o1){
        ListNode n1 = null;
        ListNode p = o1;
        while (p != null){
            n1 = new ListNode(p.val,n1);//将得到的p,放入新链表的头部
            p = p.next;
        }
        return n1;
    }

    public static void main(String[] args) {
        ListNode o5 = new ListNode(5,null);
        ListNode o4 = new ListNode(4,o5);
        ListNode o3 = new ListNode(3,o4);
        ListNode o2 = new ListNode(2,o3);
        ListNode o1 = new ListNode(1,o2);
        System.out.println(o1);
        ListNode n1 = new code().reverseList(o1);
        System.out.println(n1.toString());
    }

方法二:

public class List {//容器类
    ListNode head;

    public List(ListNode head){
        this.head = head;
    }
    public void addFirst(ListNode first){
        first.next = head;
        head = first;
    }
    public ListNode removeFirst(){
        ListNode first = head;
        if (first != null){
            head = first.next;
        }
        return first;
    }
}


    public ListNode reverseList(ListNode head){
        List list1 = new List(head);
        List list2 = new List(null);
        while (true){
            ListNode first = list1.removeFirst();//遍历旧链表,挨着移除
            if (first == null){
                break;
            }
            list2.addFirst(first);//将旧链表的值放入新链表
        }
        return list2.head;
    }

方法三:递归

    public ListNode reverseList(ListNode p){
        if (p == null || p.next == null){
            return p; //最后结点
        }
        ListNode last = reverseList(p.next);
        p.next.next=p;//建立各个值之间的关系
        p.next=null;
        return last;
    }

方法四:

    public ListNode reverseList(ListNode o1){
        //特殊条件:空链表   只有一个元素
        if (o1==null || o1.next==null){
            return o1;
        }
        ListNode o2 = o1.next;
        ListNode n1 = o1;
        while (o2!=null){
            o1.next = o2.next; //第二步
            o2.next=n1;//第三步
            n1=o2;  //第四步
            o2=o1.next; //第五步
        }
        return n1;
    }

方法五:

    public ListNode reverseList(ListNode o1){
        if (o1 == null || o1.next == null){
            return o1;
        }
        ListNode n1 = null;
        while (o1 != null){
            ListNode o2 = o1.next; //第二步
            o1.next = n1;  //第三步
            n1 = o1;  //第四步
            o1 = o2;  //第四步
        }
        return n1;
    }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值