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

    输入一个链表,反转链表后,输出链表的所有元素。

     1 /*
     2 public class ListNode {
     3     int val;
     4     ListNode next = null;
     5 
     6     ListNode(int val) {
     7         this.val = val;
     8     }
     9 }*/
    10 public class Solution {
    11     public ListNode ReverseList(ListNode head) {
    12 
    13         if (null == head || null == head.next) {
    14             return head;
    15         }
    16 
    17         ListNode lastNode = head;
    18         ListNode currNode = head.next;
    19         ListNode preNode = head.next.next;
    20 
    21         while(null != preNode){
    22             currNode.next = lastNode;
    23 
    24             if (lastNode == head) {
    25                 lastNode.next = null;
    26             }
    27 
    28             lastNode = currNode;
    29             currNode = preNode;
    30             preNode = preNode.next;
    31         } 
    32         
    33         currNode.next = lastNode;
    34 
    35         return currNode;
    36     
    37     }
    38 }
  • 相关阅读:
    希尔排序
    代理模式
    快速排序
    插入排序
    各种排序算法的稳定性和复杂度
    简单选择排序
    冒泡排序
    流程图
    PLAY学习【未完】
    项目之maven心得
  • 原文地址:https://www.cnblogs.com/jiangyi-uestc/p/5878049.html
Copyright © 2011-2022 走看看