LeetCode第92號題–反轉鏈表II
題目如下
反轉從位置 m 到 n 的鏈表。請使用一趟掃描完成反轉。
說明:
1 ≤ m ≤ n ≤ 鏈表長度。
示例:
輸入: 1->2->3->4->5->NULL, m = 2, n = 4
輸出: 1->4->3->2->5->NULL
解答
public static ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode pre = dummyHead;
//找到m前的那個結點
for (int i = 1; i < m; i++)
pre = pre.next;
//head指向需要更換的第一個數
head = pre.next;
for (int i = m; i < n; i++) {
//累計將需要更換的前1,2的結點更換
//pre->head->next->next.next ===> pre->next->head->next.next
ListNode next = head.next;
head.next = next.next;
next.next = pre.next;
pre.next = next;
}
return dummyHead.next;
}
因爲鏈表算法不好測試,所以我們自己設計鏈表來測試,根據此題,全部代碼如下:
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public class LinkedList {
private ListNode dummyHead;
private int size;
public LinkedList() {
dummyHead = new ListNode(0);
size = 0;
}
// 獲取鏈表中的元素個數
public int getSize() {
return size;
}
// 返回鏈表是否爲空
public boolean isEmpty() {
return size == 0;
}
// 在鏈表的index(0-based)位置添加新的元素e
public void add(int index, int val) {
if (index < 0 || index > size)
throw new IllegalArgumentException("Add failed. Illegal index.");
ListNode prev = dummyHead;
for (int i = 0; i < index; i++)
prev = prev.next;
prev.next = new ListNode(val, prev.next);
size++;
}
// 在鏈表頭添加新的元素val
public void addFirst(int val) {
add(0, val);
}
// 在鏈表末尾添加新的元素val
public void addLast(int val) {
add(size, val);
}
// 獲得鏈表的第index(0-based)個位置的元素
// 在鏈表中不是一個常用的操作,練習用:)
public ListNode get(int index) {
if (index < 0 || index >= size)
throw new IllegalArgumentException("Get failed. Illegal index.");
ListNode cur = dummyHead.next;
for (int i = 0; i < index; i++)
cur = cur.next;
return cur;
}
// 獲得鏈表的第一個元素
public ListNode getFirst() {
return get(0);
}
// 獲得鏈表的最後一個元素
public ListNode getLast() {
return get(size - 1);
}
// 修改鏈表的第index(0-based)個位置的元素爲val
public void set(int index, int val) {
if (index < 0 || index >= size)
throw new IllegalArgumentException("Set failed. Illegal index.");
ListNode cur = dummyHead.next;
for (int i = 0; i < index; i++)
cur = cur.next;
cur.val = val;
}
// 查找鏈表中是否有元素val
public boolean contains(int val) {
ListNode cur = dummyHead.next;
while (cur != null) {
if (cur.val == val)
return true;
cur = cur.next;
}
return false;
}
// 從鏈表中刪除index(0-based)位置的元素, 返回刪除的元素
public int remove(int index) {
if (index < 0 || index >= size)
throw new IllegalArgumentException("Remove failed. Index is illegal.");
ListNode prev = dummyHead;
for (int i = 0; i < index; i++)
prev = prev.next;
ListNode removeNode = prev.next;
prev.next = removeNode.next;
removeNode.next = null;
size--;
return removeNode.val;
}
// 從鏈表中刪除第一個元素, 返回刪除的元素
public int removeFirst() {
return remove(0);
}
// 從鏈表中刪除最後一個元素, 返回刪除的元素
public int removeLast() {
return remove(size - 1);
}
// 從鏈表中刪除元素val
public void removeElement(int val) {
ListNode prev = dummyHead;
while (prev.next != null) {
if (prev.next.val == val)
break;
prev = prev.next;
}
if (prev.next != null) {
ListNode delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
size--;
}
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
ListNode cur = dummyHead.next;
while (cur != null) {
res.append(cur.val + "->");
cur = cur.next;
}
res.append("NULL");
return res.toString();
}
public static String print(ListNode node) {
StringBuilder res = new StringBuilder();
while (node != null) {
res.append(node.val + "->");
node = node.next;
}
res.append("NULL");
return res.toString();
}
public static ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode pre = dummyHead;
//找到m前的那個結點
for (int i = 1; i < m; i++)
pre = pre.next;
//head指向需要更換的第一個數
head = pre.next;
for (int i = m; i < n; i++) {
//累計將需要更換的前1,2的結點更換
//pre->head->next->next.next ===> pre->next->head->next.next
ListNode next = head.next;
head.next = next.next;
next.next = pre.next;
pre.next = next;
}
return dummyHead.next;
}
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
for (int i = 0; i < 5; i++) {
linkedList.addLast(i);
// System.out.println(linkedList);
}
ListNode node = linkedList.get(0);
System.out.println(print(node));
ListNode node1 = reverseBetween(node,2,4);
System.out.println(print(node1));
}
}
此題是LeetCode第206號題–反轉鏈表的升級版