Pro5 從頭到尾列印鏈表(java)

來源:互聯網
上載者:User

標籤:

註:(1)這裡體現了java資料結構與C語言的不同之處

(2)棧的操作直接利用stack進行

 1 package com.xsf.SordForOffer; 2  3 import java.util.Stack; 4  5  6 /* 7  *劍指offer 8  *pro5 鏈表逆序輸出  9  * */10 11 class ListNode{12     //定義節點13     int data;14     ListNode next;15 }16 17 //利用棧反向列印18 class PrintListReverse{19     //輸入一個頭結點然後利用棧操作完成逆序輸出20     public void print_reverse(ListNode listNode){21         Stack<ListNode> stack = new Stack<ListNode>();22         //將節點入棧23         while(listNode!=null){24             stack.push(listNode);25             listNode = listNode.next;26         }27         //節點出棧28         while(!stack.empty()){29             System.out.println(stack.pop().data);30         }31     }32 33 }34 public class Pro5LinklistReverse {35     public static void main(String[] args) {36         ListNode node1 = new ListNode();37         ListNode node2 = new ListNode();38         ListNode node3 = new ListNode();39         node1.data = 1;40         node2.data = 3;41         node3.data = 5;42         node1.next = node2;43         node2.next = node3;44         PrintListReverse test = new PrintListReverse();45         test.print_reverse(node1);46     }47 }

 

Pro5 從頭到尾列印鏈表(java)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.