In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to print linked list in Java". In daily operation, I believe many people have doubts about how to print linked list in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to print linked list in Java". Next, please follow the editor to study!
Title
Enter a linked list and return an ArrayList in the order from end to end of the list.
Analysis.
Just look at this topic, a little do not know how to start the feeling, the meaning is clear, but how to do it? First of all, we prepare a linked list, the title is prompted, and we create a ListNode class with the following contents
Public class ListNode {
Int val
ListNode next = null
ListNode (int val) {
This.val = val
}
}
This can be used to represent a linked list, for example, we create a 1, 3, 5, 8 linked list
ListNode listNode=new ListNode (1)
ListNode.next=new ListNode (3)
ListNode.next.next=new ListNode (5)
ListNode.next.next.next=new ListNode (8)
Well, the preparatory work is done. Let's take a look at the title next. There are two ways to load the elements of the linked list into list from beginning to end. According to the last-in-first-out principle of the stack, the linked list data is pressed into the stack, and then the data is taken out from the stack and stored in list. Another way is to use the recursive method to find the last node of the linked list and move forward in turn. I'm using the recursive method here.
Solution method
Using recursion, we use recursive methods to find the last element in the linked list and add it to the list.
Public static ArrayList printListFromTailToHead (ListNode listNode) {
ArrayList list=new ArrayList ()
If (listNodeparts null) {
Add (list,listNode)
}
Return list
}
Public static void add (ArrayList list,ListNode listNode) {
/ / add the element to the list if there is no subsequent node, and recursively find the subsequent node if there is one
If (listNode.nextroomnull) {
Add (list,listNode.next)
}
List.add (listNode.val)
}
Source code package com.quellanan.algorithm.day2
Import java.util.ArrayList
/ * *
* @ ClassName Solution
* @ Description DOTO
* @ Author zhulinfeng
* @ Date 11:38 on 2020-1-30
* @ Version 1.0
, /
Public class Solution {
Public static void main (String [] args) {
ListNode listNode=new ListNode (1)
ListNode.next=new ListNode (3)
ListNode.next.next=new ListNode (5)
ListNode.next.next.next=new ListNode (8)
ArrayList list=printListFromTailToHead (listNode)
While (listNode.nextroomnull) {
System.out.print (listNode.val+ "\ t")
ListNode=listNode.next
}
System.out.println (listNode.val)
For (int iTuno Bandi)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.