zoukankan      html  css  js  c++  java
  • NC78 反转链表

    package NC;

    /**
    * NC78 反转链表
    *
    * 输入一个长度为n链表,反转链表后,输出新链表的表头。
    *
    * 数据范围
    * 要求:空间复杂度O(1),时间复杂度O(n) 。
    *
    * @author Tang
    * @date 2021/9/24
    */
    public class ReverseList {

    public ListNode ReverseList(ListNode head) {
    ListNode newHead = head;
    ListNode temp = null;

    ListNode next = newHead.next;
    //找到最后一个元素
    while(next != null) {
    temp = newHead;
    newHead = next;
    next = newHead.next;
    newHead.next = temp;
    }

    head.next = null;
    return newHead;
    }

    public static void main(String[] args) {


    }

    }

    class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
    this.val = val;
    }
    }
  • 相关阅读:
    问题2017S03
    问题2017S02
    高等代数问题1
    无穷积分换元法的严格解释
    线性空间的同构理论
    问题2017S01
    朴素贝叶斯分类
    决策树
    温习MATLAB
    感知机
  • 原文地址:https://www.cnblogs.com/ttaall/p/15330695.html
Copyright © 2011-2022 走看看