Java_實現單鏈表-基本操作


一、通過JAVA實現單鏈表

  增刪改查、返回長度、反轉、查找、排序

二、代碼

  1 package officeCoding;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Stack;
  5 
  6 /**
  7  * 從尾到頭遍歷鏈表 輸入一個鏈表,按鏈表值從尾到頭的順序返回一個ArrayList
  8  * 
  9  * @author Administrator
 10  */
 11 class ListNode {// 單鏈表節點構建
 12     int val;
 13     ListNode next = null;
 14 
 15     ListNode(int val) {
 16         this.val = val;
 17     }
 18 }
 19 
 20 public class Pro_03 {
 21 
 22     static ListNode head = null;// 創建一個頭節點
 23 
 24     public static void main(String[] args) {
 25         addNode(5);
 26         addNode(8);
 27         ArrayList<Integer> list = printListFromTailToHead(head);
 28         System.out.println(list);
 29     }
 30 
 31     // 隊列和棧是一對好基友,從尾到頭打印鏈表,當然離不開借助棧的幫忙啦
 32     // 所以,先把鏈表里的東西,都放到一個棧里去,然后按順序把棧里的東西pop出來,就這么簡單
 33     public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
 34         Stack<Integer> stack = new Stack<Integer>();
 35         while (listNode != null) {
 36             stack.push(listNode.val);
 37             listNode = listNode.next;
 38         }
 39         ArrayList<Integer> list = new ArrayList<Integer>();
 40         while (!stack.isEmpty()) {
 41             list.add(stack.pop());
 42         }
 43         return list;
 44     }
 45 
 46     // input
 47     public static void addNode(int d) {
 48         ListNode newNode = new ListNode(d);
 49         if (head == null) {
 50             head = newNode;
 51         }
 52         ListNode tmp = head;
 53         while (tmp.next != null) {
 54             tmp = tmp.next;
 55         }
 56         tmp.next = newNode;
 57     }
 58 
 59     // delete
 60     public boolean deleteNode(int index) {
 61         if (index < 1 || index > length()) {
 62             return false;// 如果當前index在鏈表中不存在
 63         }
 64         if (index == 1) {// 如果index指定的是頭節點
 65             head = head.next;
 66             return true;
 67         }
 68         int i = 2;
 69         ListNode preNode = head;// 前一個節點(從頭節點開始)
 70         ListNode curNode = preNode.next;// 當前節點
 71         while (curNode != null) {
 72             if (i == index) {
 73                 preNode.next = curNode.next;// 刪除當節點,前節點連接到下節點
 74                 return true;
 75             }
 76             preNode = curNode;
 77             curNode = curNode.next;
 78             i++;
 79         }
 80         return false;
 81     }
 82 
 83     // 返回節點長度
 84 
 85     public int length() {
 86         int length = 0;
 87         ListNode tmp = head;
 88         while (tmp != null) {
 89             length++;
 90             tmp = tmp.next;
 91         }
 92         return length;
 93     }
 94 
 95     // 鏈表反轉
 96 
 97     public ListNode ReverseIteratively(ListNode head) {
 98         ListNode pReversedHead = head;
 99         ListNode pNode = head;
100         ListNode pPrev = null;
101         while (pNode != null) {
102             ListNode pNext = pNode.next;
103             if (pNext == null) {
104                 pReversedHead = pNode;
105             }
106             pNode.next = pPrev;
107             pPrev = pNode;
108             pNode = pNext;
109         }
110         this.head = pReversedHead;
111         return this.head;
112     }
113 
114     // 查找單鏈表的中間節點
115 
116     public ListNode SearchMid(ListNode head) {
117         ListNode p = this.head, q = this.head;
118         while (p != null && p.next != null && p.next.next != null) {
119             p = p.next.next;
120             q = q.next;
121         }
122         System.out.println("Mid:" + q.val);
123         return q;
124     }
125 
126     // 查找倒數 第k個元素
127 
128     public ListNode findElem(ListNode head, int k) {
129         if (k < 1 || k > this.length()) {
130             return null;
131         }
132         ListNode p1 = head;
133         ListNode p2 = head;
134         for (int i = 0; i < k; i++)// 前移k步
135             p1 = p1.next;
136         while (p1 != null) {
137             p1 = p1.next;
138             p2 = p2.next;
139         }
140         return p2;
141     }
142 
143     // 排序
144 
145     public ListNode orderList() {
146         ListNode nextNode = null;
147         int tmp = 0;
148         ListNode curNode = head;
149         while (curNode.next != null) {
150             nextNode = curNode.next;
151             while (nextNode != null) {
152                 if (curNode.val > nextNode.val) {
153                     tmp = curNode.val;
154                     curNode.val = nextNode.val;
155                     nextNode.val = tmp;
156                 }
157                 nextNode = nextNode.next;
158             }
159             curNode = curNode.next;
160         }
161         return head;
162     }
163 
164     // 從尾到頭輸出單鏈表,采用遞歸方式實現
165 
166     public void printListReversely(ListNode pListHead) {
167         if (pListHead != null) {
168             printListReversely(pListHead.next);
169             System.out.println("printListReversely:" + pListHead.val);
170         }
171     }
172 
173     // 判斷鏈表是否有環,單向鏈表有環時,尾節點相同
174 
175     public boolean IsLoop(ListNode head) {
176         ListNode fast = head, slow = head;
177         if (fast == null) {
178             return false;
179         }
180         while (fast != null && fast.next != null) {
181             fast = fast.next.next;
182             slow = slow.next;
183             if (fast == slow) {
184                 System.out.println("該鏈表有環");
185                 return true;
186             }
187         }
188         return !(fast == null || fast.next == null);
189     }
190 
191     // 找出鏈表環的入口
192 
193     public ListNode FindLoopPort(ListNode head) {
194         ListNode fast = head, slow = head;
195         while (fast != null && fast.next != null) {
196             slow = slow.next;
197             fast = fast.next.next;
198             if (slow == fast)
199                 break;
200         }
201         if (fast == null || fast.next == null)
202             return null;
203         slow = head;
204         while (slow != fast) {
205             slow = slow.next;
206             fast = fast.next;
207         }
208         return slow;
209     }
210 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM