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 }

 

posted @ 2018-11-26 10:23  wangchuanli  阅读(1696)  评论(0编辑  收藏  举报