java LinkedList的實例詳解
java LinkedList的實例詳解
站在Java的角度看,玩隊列不就是玩對象引用對象嘛!
實例代碼:
public class LinkedList<E> implements List<E>, Deque<E> { Node<E> first; Node<E> last; int size; public boolean add(E e) { final Node<E> l = last; final Node<E> newNode = new Node<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode; size++; modCount++; return true; } private static class Node<E> { E item; Node<E> next; Node<E> prev; Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; } } }
單鏈表反轉(zhuǎn):
/** * 遞歸,在反轉(zhuǎn)當前節(jié)點之前先反轉(zhuǎn)后續(xù)節(jié)點 */ public static Node reverse(Node head) { if (null == head || null == head.getNextNode()) { return head; } Node reversedHead = reverse(head.getNextNode()); head.getNextNode().setNextNode(head); head.setNextNode(null); return reversedHead; } /** * 遍歷,將當前節(jié)點的下一個節(jié)點緩存后更改當前節(jié)點指針 * */ public static Node reverse2(Node head) { if (null == head) { return head; } Node pre = head; Node cur = head.getNextNode(); Node next; while (null != cur) { next = cur.getNextNode(); cur.setNextNode(pre); pre = cur; cur = next; } //將原鏈表的頭節(jié)點的下一個節(jié)點置為null,再將反轉(zhuǎn)后的頭節(jié)點賦給head head.setNextNode(null); head = pre; return head; }
對于數(shù)組問題,一般我們要新建數(shù)組,必要時移動下標
以上就是java LinkedList 的實例,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
如何使用Sentry 監(jiān)控你的Spring Boot應用
這篇文章主要介紹了如何使用Sentry 監(jiān)控你的Spring Boot應用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11Spring mvc Json處理實現(xiàn)流程代碼實例
這篇文章主要介紹了Spring mvc Json處理實現(xiàn)流程代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09SpringBoot+thymeleaf+Echarts+Mysql 實現(xiàn)數(shù)據(jù)可視化讀取的示例
本文主要介紹了SpringBoot+thymeleaf+Echarts+Mysql 實現(xiàn)數(shù)據(jù)可視化讀取的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04Set接口深入剖析之HashSet、LinkedHashSet和TreeSet
這篇文章主要介紹了Set接口深入剖析之HashSet、LinkedHashSet和TreeSet,LinkedHashSet是HashSet的子類,實現(xiàn)了Set接口,LinkedHashSet底層是一個LinkedHashMap,底層維護了一個數(shù)組+雙向鏈表,需要的朋友可以參考下2023-09-09collection集合體系與并發(fā)修改異常的解決方法
今天小編就為大家分享一篇關(guān)于collection集合體系與并發(fā)修改異常的解決方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03