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号题–反转链表的升级版