首页 > 编程语言 >算法题:链表反转

算法题:链表反转

时间:2023-02-20 17:24:11浏览次数:33  
标签:Node head return 反转 value next 链表 算法 public

node节点:

public class Node {
    Node next;
    Integer value;

    public Node(Integer value) {
        this.value = value;
    }

    public Node addNode(Integer value) {
        Node node = new Node(value);
        this.next = node;
        return node;
    }

    public Node(Integer value, Node next) {
        this.next = next;
        this.value = value;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Node{" +
                "next=" + next +
                ", value=" + value +
                '}';
    }
}

 main函数:

 Node node1 = new Node(1);
        Node node2 = node1.addNode(2);
        Node node3 = node2.addNode(3);
        Node node4 = node3.addNode(4);
        Node node5 = node4.addNode(5);
        Node node6 = node5.addNode(6);
        Node node7 = node6.addNode(7);

 

1、循环算法:

    private Node reverseNode(Node node1) {
        Node pre = null;
        Node curr = node1;
        Node next;
        while (curr != null) {
            next = curr.next;
            curr.next = pre;
            pre = curr;
            curr = next;
        }
        return pre;
    }

 2、递归算法:

  private Node reverseNode1(Node head) {
        if(head==null || head.next==null){
            return head;
        }
        Node result = reverseNode1(head.next);
        head.next.next=head;
        head.next=null;
        return result;
    }

 

标签:Node,head,return,反转,value,next,链表,算法,public
From: https://www.cnblogs.com/niun/p/17138205.html

相关文章

  • 基于用户的协同推荐算法
    基于用户的协同推荐算法。这个算法是最早诞生的推荐算法的一种。下面就简单介绍一下它的思想和原理。一、基本思想大家在日常使用的一些App中,相信也或多或少地遇到过基于......
  • 万字长文浅析Java集合中的排序算法
    作者:京东物流秦彪1. 引言排序是一个Java开发者,在日常开发过程中随处可见的开发内容,Java中有丰富的API可以调用使用。在Java语言中,作为集合工具类的排序方法,必定要做到通......
  • Remove Nth Node From End of List 删除链表倒数第N个节点
    RemoveNthNodeFromEndofListGivenalinkedlist,removethe nthForexample,Givenlinkedlist:1->2->3->4->5,andn=2.Afterremovingthesecondnode......
  • hihoCoder 1098 : 最小生成树二·Kruscal算法
    #1098:最小生成树二·Kruscal算法10000ms1000ms256MB描述随着小Hi拥有城市数目的增加,在之间所使用的Prim算法已经无法继续使用了——但是幸运的是,经过计算机的......
  • hihoCoder 1097 : 最小生成树一·Prim算法
    #1097:最小生成树一·Prim算法10000ms1000ms256MB描述最近,小Hi很喜欢玩的一款游戏模拟城市开放出了新Mod,在这个Mod中,玩家可以拥有不止一个城市了!但是,问题也接踵......
  • hihoCoder 1089 : 最短路径·二:Floyd算法
    #1089:最短路径·二:Floyd算法10000ms1000ms256MB描述万圣节的中午,小Hi和小Ho在吃过中饭之后,来到了一个新的鬼屋!鬼屋中一共有N个地点,分别编号为1..N,这N个地点之......
  • 算法之字符串
    字符串字符串--反转字符串题目:力扣题目链接(opensnewwindow)编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组char[]的形式给出。不要给另外的......
  • 算法刷题 Day 44 | ● 完全背包 ● 518. 零钱兑换 II ● 377. 组合总和 Ⅳ
    力扣上没有纯粹的完全背包的题目,所以大家看本篇了解一下完全背包的理论后面的两道题目,都是完全背包的应用,做做感受一下完全背包视频讲解:https://www.bilibili.c......
  • 算法基础模板
    时空复杂度分析一般ACM或者笔试题的时间限制是1秒或2秒。在这种情况下,C++代码中的操作次数控制在107~108为最佳。下面给出在不同数据范围下,代码的时间复杂度和算法该如何选......
  • 图的最短路径算法
    1.不带权值的最短路径对于不带权值的最短路径而言,我们可以采用广度优先遍历的方法,同时在遍历的过程中记录其上一个节点即可。如下图所示,我们找寻从A顶点到H顶点的最短......