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         if(head==null)
    13             return null;
    14         else if(head.next==null)
    15             return head;
    16         else{
    17             ListNode p = head.next;
    18             ListNode temp = head;
    19             head = head.next;
    20             temp.next = null;
    21             while(head.next!=null){
    22                 head = head.next;
    23                 p.next = temp;
    24                 temp = p;
    25                 p = head;
    26             }
    27             head.next = temp;
    28             return head;
    29         }
    30 
    31     }
    32 }
  • 相关阅读:
    flask1 + jinja2 day88
    linux9
    linux8 redis集群槽+docker
    dsadfa
    redis
    aaa
    a
    题目
    java对含有中文的字符串进行Unicode编码
    Java转Double类型经纬度为度分秒格式
  • 原文地址:https://www.cnblogs.com/haq123/p/12154784.html
Copyright © 2011-2022 走看看