本文主要是介绍建立升序链表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目1181:遍历链表import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner cin = new Scanner(new InputStreamReader(System.in)) ;PrintWriter cout = new PrintWriter(System.out) ;while(cin.hasNext()){new Solve(cin.nextInt()).solve(cin, cout) ; }cout.flush() ; cout.close() ; } }class Node{int val ; Node next ;Node(int val){this.val = val ;next = null ;}Node(){next = null ;}
}class Solve{Node head = null ;int n ;Solve(int n){this.n = n ;}void solve(Scanner cin , PrintWriter cout){head = new Node() ;for(int i = 1 ; i <= n ; i++){Node p = head ;Node now = new Node(cin.nextInt()) ; while(p.next != null && p.next.val <= now.val)p = p.next ;Node pnext = p.next ;p.next = now ;now.next = pnext ;}Node p = head.next ;cout.print(p.val) ;while(p.next != null){p = p.next ;cout.print(" " + p.val) ;}cout.println() ;// cout.flush(); }}
这篇关于建立升序链表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!