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

    /**
     * @program: IdeaJava
     * @Date: 2019/12/23 17:32
     * @Author: lhh
     * @Description: 输入一个链表,反转链表后,输出新链表的表头。
     */
     class ListNode {
        int val;
        ListNode next = null;
    
        ListNode(int val) {
            this.val = val;
        }
    }
    public class Solution14ReverseList {
        public static ListNode reverseList(ListNode head)
        {
            ListNode pre = null;
            ListNode p = null;
            if(head == null || head.next == null)return head;
            else
            {
                while(head != null)
                {
                    p = head.next;
                    head.next = pre;
                    pre = head;
                    head = p;
                }
                return pre;
            }
        }
    
        public static void showListNode(ListNode head)
        {
            while(head != null)
            {
                System.out.print(head.val+" ");
                head = head.next;
            }
            System.out.println();
        }
        public static void main(String[] args) {
            ListNode a = new ListNode(1);
            ListNode b = new ListNode(2);
            ListNode c = new ListNode(3);
            ListNode d = new ListNode(4);
            ListNode e = new ListNode(5);
            a.next = b;
            b.next = c;
            c.next = d;
            d.next = e;
            showListNode(a);
            showListNode(reverseList(a));
        }
    
    }
  • 相关阅读:
    51Nod1119
    stoi
    坑爹大质数
    USACO07OPEN Cheapest Palindrome
    USACO08NOV Mixed Up Cows
    USACO12FEB Nearby Cows
    SCOI2009 粉刷匠
    USACO16OPEN 248
    POI2014 PTA-Little Bird
    USACO17FEB Why Did the Cow Cross the Road I G
  • 原文地址:https://www.cnblogs.com/lhh666/p/12089804.html
Copyright © 2011-2022 走看看