指定区间反转
LeetCode92:给你单链表的头指针 head 和两个整数 left和 right,其中 left <= right。请你反转从位置 left 到位置 right 的链表节点,返回反转后的链表。
示例:
输入:head = 1->2->3->4->5,left = 2,right = 4
输出: 1->4->3->2->5
方法一:头插法
整体思路类型与上一篇讲到的利用虚拟节点的办法,区别在于,这一次的虚拟节点不一定是空的,首先要找到left的上一个节点,就是虚拟节点dummyNode
代码如下:
public static ListNode reverseBetween2(ListNode head, int left, int right) {
// 设置 dummyNode 是这一类问题的一般做法
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
ListNode pre = dummyNode;
//根据left找到虚拟节点的位置
for (int i = 0; i < left - 1; i++) {
pre = pre.next;
}
//找到第一个需要反转节点
ListNode cur = pre.next;
ListNode next;
for (int i = 0; i < right - left; i++) {
//存储cur的next节点信息
next = cur.next;
//当前节点的next指针指向,下下个节点,方便后续链表的调转
cur.next = next.next;
//前两个节点反转
next.next = pre.next;
pre.next = next;
}
return dummyNode.next;
}
方法二:穿针引线法
该方法的整体思路是先找到pre,left,right,succ四个节点,之后将要反转的区域(left,right)裁剪下来,将裁剪下来的链表反转后,在拼接回原链表上即可。
代码如下:
/**
* 方法1:穿针引线法
*
* @param head
* @param left
* @param right
* @return
*/
public static ListNode reverseBetween(ListNode head, int left, int right) {
// 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
ListNode pre = dummyNode;
// 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点
// 建议写在 for 循环里,语义清晰
for (int i = 0; i < left - 1; i++) {
pre = pre.next;
}
// 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点
ListNode rightNode = pre;
for (int i = 0; i < right - left + 1; i++) {
rightNode = rightNode.next;
}
// 第 3 步:切断出一个子链表(截取链表)
ListNode leftNode = pre.next;
ListNode succ = rightNode.next;
// 注意:切断链接
pre.next = null;
rightNode.next = null;
// 第 4 步:同第 206 题,反转链表的子区间
reverseList(leftNode);
// 第 5 步:接回到原来的链表中
pre.next = rightNode;
leftNode.next = succ;
return dummyNode.next;
}
/**
* 基本的反转方法
*
* @param head
* @return
*/
public static ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
两两交换链表中的节点
LeetCode24.给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
public static ListNode swapPairs(ListNode head) {
//定义虚拟节点
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode cur = dummyHead;
//如果当前节点的后两个节点都不空,进行两两反转
while (cur.next != null && cur.next.next != null) {
//node1为下一个
ListNode node1 = cur.next;
//node为下下个
ListNode node2 = cur.next.next;
//两两交换,当前节点的next指针指向node2
cur.next = node2;
//node1的next指针指向下一个需要反转的两个节点的头部
node1.next = node2.next;
//node2的next指针指向node1
node2.next = node1;
//到此为止两个节点互换完毕
//将curr节点更新到需要两两互换节点的上一个节点
cur = node1;
}
return dummyHead.next;
}
单链表加1
LeetCode369.用一个非空单链表来表示一个非负整数,然后将这个整数加一。你可以假设这个整数除了0本身,没有任何前导的 0。这个整数的各个数位按照 高位在链表头部、低位在链表尾部 的顺序排列。
示例: 输入:1,2,9
输出:1,3,0
代码如下:
public static ListNode plusOne(ListNode head) {
Stack<Integer> st = new Stack();
//压栈,将链表里的节点放入栈里
while (head != null) {
st.push(head.val);
head = head.next;
}
//进位数,除了0就是1
int carry = 0;
ListNode dummy = new ListNode(0);
//要加的数
int adder = 1;
//栈不为空,有加数,进位大于零,只要满足任意一个条件,就要循环
while (!st.empty() || adder != 0 || carry > 0) {
//取出低位(栈顶元素),如果栈为空,进来循环,说明有没加的数或者没有进位的数
int digit = st.empty() ? 0 : st.pop();
//求当前位置的和:栈顶元素+加数+进位
int sum = digit + adder + carry;
//如果和大于10则进一位
carry = sum >= 10 ? 1 : 0;
//如果和大于10,则减十,留下剩余
sum = sum >= 10 ? sum - 10 : sum;
//将处理后的sum放入新的节点
ListNode cur = new ListNode(sum);
cur.next = dummy.next;
dummy.next = cur;
adder = 0;
}
return dummy.next;
}
两个链表求和
LeetCode445题,给你两个非空链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。你可以假设除了数字 0之外,这两个数字都不会以零开头。示例:
输入:
链表1:6,1,7
链表2:2,9,5
输出:9,1,2
即617+295 = 912
方法一:通过栈实现
类似于上一题给链表+1,将两个链表分别压入两个栈中,然后结果使用头插法返回即可。
代码如下:
public static ListNode addInListByStack(ListNode head1, ListNode head2) {
//链表压栈
Stack<ListNode> st1 = new Stack<ListNode>();
Stack<ListNode> st2 = new Stack<ListNode>();
while (head1 != null) {
st1.push(head1);
head1 = head1.next;
}
while (head2 != null) {
st2.push(head2);
head2 = head2.next;
}
//因为要使用头插法,先创建一个虚拟节点
ListNode newHead = new ListNode(-1);
//进位值
int carry = 0;
//这里设置carry!=0,是因为当st1,st2都遍历完时,如果carry=0,就不需要进入循环了
while (!st1.empty() || !st2.empty() || carry != 0) {
ListNode a = new ListNode(0);
ListNode b = new ListNode(0);
if (!st1.empty()) {
a = st1.pop();
}
if (!st2.empty()) {
b = st2.pop();
}
//每次的和应该是对应位相加再加上进位
int get_sum = a.val + b.val + carry;
//对累加的结果取余
int ans = get_sum % 10;
//如果大于0,就进位
carry = get_sum / 10;
ListNode cur = new ListNode(ans);
cur.next = newHead.next;
//每次把最新得到的节点更新到neHead.next中
newHead.next = cur;
}
return newHead.next;
}
方法二:使用链表反转实现
现将两个链表分别反转,最后计算完之后,将结果再次反转, 总共需要三次反转
代码如下:
public static ListNode addInListByReverse(ListNode head1, ListNode head2) {
//反转链表
head1 = reverse(head1);
head2 = reverse(head2);
ListNode head = new ListNode(-1);
ListNode cur = head;
int carry = 0;
while (head1 != null || head2 != null) {
int val = carry;
if (head1 != null) {
val += head1.val;
head1 = head1.next;
}
if (head2 != null) {
val += head2.val;
head2 = head2.next;
}
cur.next = new ListNode(val % 10);
carry = val / 10;
cur = cur.next;
}
//循环完成后,如果进位还有值,则把进位的值放入新的节点中,在插入cur的下一个指针,反转之后就是最高位了
if (carry > 0) {
cur.next = new ListNode(carry);
}
return reverse(head.next);
}
private static ListNode reverse(ListNode head) {
ListNode cur = head;
ListNode pre = null;
while (cur != null) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
链表的回文序列问题
之前使用栈实现过一次,这次使用快慢指针+一半反转法去实现。
代码如下
public static boolean isPalindromeByTwoPoints(ListNode head) {
if (head == null || head.next == null) {
return true;
}
ListNode slow = head, fast = head;
ListNode pre = head, prepre = null;
//将链表的一半(慢指针经过的地方)反转,当快指针到结尾时,停止反转
while (fast != null && fast.next != null) {
//快慢指针+链表反转
pre = slow;
slow = slow.next;
fast = fast.next.next;
pre.next = prepre;
prepre = pre;
}
if (fast != null) {
slow = slow.next;
}
while (pre != null && slow != null) {
//此时pre指针在的位置是前半段,并且反转后的链表
//slow是中间节点
if (pre.val != slow.val) {
return false;
}
pre = pre.next;
slow = slow.next;
}
return true;
}
总结
在处理链表相关问题时,双指针和链表反转是非常有用的方法,可以解决许多情况下的问题。当遇到链表问题时,推荐使用这两种方法进行尝试。同时,建议在编写代码时结合画图的方式进行,这样可以更好地理解问题和编写代码,而不是死记硬背特定的代码。记住,理解和思考问题是解决问题的关键。