我的簡書:http://www.lxweimin.com/u/c91e642c4d90
我的CSDN:http://blog.csdn.net/wo_ha
我的GitHub:https://github.com/chuanqiLjp
我的個人博客:https://chuanqiljp.github.io/
版權(quán)聲明:商業(yè)轉(zhuǎn)載請聯(lián)系我獲得授權(quán),非商業(yè)轉(zhuǎn)載請在醒目位置注明出處。
1. 單鏈表反轉(zhuǎn);
public Node inverseLinkList(Node head) {//將頭結(jié)點為head的鏈表進行反轉(zhuǎn),返回反轉(zhuǎn)后的頭結(jié)點的鏈表
Node pre_node = null;
Node cur_node = head;
Node nex_node = null;
if (head == null || head.next == null) return head;
while (cur_node != null) {
nex_node = cur_node.next;
cur_node.next = pre_node;
pre_node = cur_node;
cur_node = nex_node;
}
return pre_node;
}
2. 鏈表中環(huán)的檢測;
/**
* 檢測鏈表中的環(huán)形: 快慢指針法
* 首先設(shè)置兩個指針,分別命名為fast和slow,fast指針每次向后移2步,slow指針每次向后移1步。如果,fast指針最后走到尾結(jié)點,則沒有環(huán)。如果,fast指針和slow指針相遇,則證明有環(huán)。
* 環(huán)的起始結(jié)點的查詢: 當(dāng)fast與slow相遇之后,fast指針從頭結(jié)點開始走,每次走1步,當(dāng)fast再次與slow相遇以后,相遇處的結(jié)點為環(huán)的入口結(jié)點
*
* @param head 待檢測的鏈表
* @return 環(huán)結(jié)點的入口, null表示無環(huán)結(jié)點
*/
public Node checkLinkRing_v1(final Node head) {
Node ringIn_node = null;
if (head == null || head.next == null || head.next.next == null) {
return null;
}
Node fast_node = head;
Node slow_node = head;
int count = 0;//相遇的次數(shù)
while (fast_node != null && slow_node != null) {
if (fast_node.next == null) {//走到了尾結(jié)點,不可能有環(huán)
break;
}
if (ringIn_node == null) {
fast_node = fast_node.next.next;
} else {
if (count == 1) {
fast_node = head;
}
fast_node = fast_node.next;
}
slow_node = slow_node.next;
if (fast_node == slow_node) {
count++;
ringIn_node = fast_node;
if (count == 2) {
break;
}
}
}
return ringIn_node;
}
/**
* 足跡法: 順序遍歷鏈表中所有的節(jié)點,并將所有遍歷過的節(jié)點信息保存下來。如果某個節(jié)點的信息出現(xiàn)了兩次,則存在環(huán)。
* @param head
* @return
*/
public boolean checkLinkRing_v2(final Node head) {
boolean result = false;
HashMap<Node, Node> map = new HashMap<>();
Node p = head;
while (p != null) {
if (map.containsKey(p)) {
return true;
}
map.put(p, p);
p = p.next;
}
return result;
}
3. 兩個有序的鏈表合并;
/**
* 通過比較,每次只拷貝小的數(shù)據(jù)到合并后的鏈表中,原有鏈表結(jié)構(gòu)不會被破壞
*
* @param h1
* @param h2
* @return 合并后的鏈表的頭
*/
public static Node mergeOrderlyLink_v1(final Node h1, final Node h2) {
Node tem = h1 != null ? h1 : h2;
if (h1 == null || h2 == null) {
return tem;
}
Node merge = null;
Node p1 = h1, p2 = h2;
Node pm = new Node(-1, null);//自定義一個頭結(jié)點
int i = 0;
while (p1 != null && p2 != null) {//比較大小,將小的拷貝到合并后的鏈表中
if (p1.data <= p2.data) {
pm.next = new Node(p1.data, null);
p1 = p1.next;
} else {
pm.next = new Node(p2.data, null);
p2 = p2.next;
}
if (i == 0) {
merge = pm;
}
pm = pm.next;
i++;
}
tem = p1 == null ? p2 : p1;
while (tem != null) {//拷貝剩余的數(shù)據(jù)追加到合并后的鏈表中
pm.next = new Node(tem.data, null);
pm = pm.next;
tem = tem.next;
}
merge = merge.next;//去除自定義的頭結(jié)點
return merge;
}
/**
* 遞歸合并有序鏈表,原有鏈表結(jié)構(gòu)會被破壞,數(shù)據(jù)較大時容易發(fā)生堆棧溢出異常,
* 說明參考鏈接:https://blog.csdn.net/fengpojian/article/details/81384130實現(xiàn)
*
* @param h1
* @param h2
* @return
*/
public static Node mergeOrderlyLink_v2(final Node h1, final Node h2) {
Node tem = h1 != null ? h1 : h2;
if (h1 == null || h2 == null) {
return tem;
}
Node merge = null;
if (h1.data <= h2.data) {
merge = h1;
merge.next = mergeOrderlyLink_v2(h1.next, h2);
} else {
merge = h2;
merge.next = mergeOrderlyLink_v2(h1, h2.next);
}
return merge;
}
4. 刪除鏈表倒數(shù)第 n 個結(jié)點(n 保證有效);
/**
* 一次遍歷法: 使用快慢指針。快指針比慢指針提前n個單元。當(dāng)快指針到達單鏈表尾部時,慢指針指向待刪除節(jié)點的前節(jié)點。
*
* @param head
* @param beforIndex 保證有效,從 1 開始
* @return
*/
public static Node deleteIndexFormEnd_v2(final Node head, int beforIndex) {
Node fast = head;
Node slow = head;
for (int i = 0; i < beforIndex; i++) {
fast = fast.next;
}
if (fast == null) {
return head.next;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
/**
* 常規(guī)做法: 遍歷第一次求出鏈表的長度,遍歷第二次刪除
*
* @param head
* @param beforIndex 保證有效,從 1 開始
* @return
*/
public static Node deleteIndexFormEnd_v1(final Node head, int beforIndex) {
int size = 0;
Node p = head;
while (p != null) {
p = p.next;
size++;
}
if (beforIndex == size) {//刪除頭結(jié)點
return head.next;
}
int count = 0;
int k = size - beforIndex;
p = head;//復(fù)位
Node pre = p; //記錄要尋找到的刪除的結(jié)點的上結(jié)點
while (p != null) {
if (count == k) {//找到要刪除的結(jié)點
pre.next = p.next;
break;
}
pre = p;
p = p.next;
count++;
}
return head;
}
5. 求鏈表的中間結(jié)點;
/**
* 尋找鏈表的中間結(jié)點: 設(shè)置兩個指針,一個快指針,每次走兩步,一個慢指針,每次走一步。
* 常規(guī)做法: 先遍歷整個鏈表求長度在遍歷一次找到中間結(jié)點,代碼省略
*
* @param head
* @return
*/
public static Node findCenterNode(Node head) {
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}