數據結構相關
阿新 • • 發佈:2017-06-06
索引 get 鏈表 一樣在 nbsp clas -1 特點 for
鏈表特點:
涉及到索引角標的增刪改查操作,都需要先查到元素,然後才可以做其他操作,這種操作的復雜度是O(N);
鏈表一般會定義頭尾指針,頭尾的增刪很方便,很適合用於實現隊列(只有首尾操作)。
鏈表由於可以頭插入和尾插入等等,所以實現逆序很方便,只需要遍歷並進行依次頭插入即可實現反轉鏈表,也可以通過反轉指針實現。
1 public void reverse() { 2 // temp 和next的位置像斐波那契一樣在依次向後移動 3 Node temp = first; 4 last = first; 5 Node next = temp.getNext();反轉指針6 for(int i = 0; i < size - 1; i++) { 7 Node nextNext = next.getNext(); 8 next.setNext(temp); 9 temp = next; 10 next = nextNext; 11 } 12 first = temp; 13 last.setNext(null); 14 }
數據結構相關