目录
面试题 02.04. 分割链表
面试题 02.05. 链表求和
面试题 02.06. 回文链表
面试题 02.07. 链表相交
面试题 02.04. 分割链表
面试题 02.04. 分割链表
给你一个链表的头节点 head
和一个特定值 x
,请你对链表进行分隔,使得所有 小于 x
的节点都出现在 大于或等于 x
的节点之前。
你不需要 保留 每个分区中各节点的初始相对位置。
示例 1:
输入:head = [1,4,3,2,5,2], x = 3 输出:[1,2,2,4,3,5]
示例 2:
输入:head = [2,1], x = 2 输出:[1,2]
提示:
- 链表中节点的数目在范围
[0, 200]
内 -100 <= Node.val <= 100
-200 <= x <= 200
java">class Solution {public ListNode partition(ListNode head, int x) {if (head == null) {return head;}// 创建虚拟头节点ListNode leftHead = new ListNode();ListNode rightHead = new ListNode();ListNode left = leftHead;ListNode right = rightHead;while (head != null) {if (head.val < x) {left.next = head;left = left.next;} else {right.next = head;right = right.next;}head = head.next; // 移动原链表指针}// 连接两个链表left.next = rightHead.next;right.next = null;return leftHead.next;}
}
面试题 02.05. 链表求和
面试题 02.05. 链表求和
给定两个用链表表示的整数,每个节点包含一个数位。
这些数位是反向存放的,也就是个位排在链表首部。
编写函数对这两个整数求和,并用链表形式返回结果。
示例:
输入:(7 -> 1 -> 6) + (5 -> 9 -> 2),即617 + 295 输出:2 -> 1 -> 9,即912
进阶:思考一下,假设这些数位是正向存放的,又该如何解决呢?
示例:
输入:(6 -> 1 -> 7) + (2 -> 9 -> 5),即617 + 295 输出:9 -> 1 -> 2,即912
java">class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {if (l1 == null) return l2; // 如果l1为空,直接返回l2if (l2 == null) return l1; // 如果l2为空,直接返回l1ListNode dummy = new ListNode(0); // 创建虚拟头节点ListNode current = dummy;int carry = 0; // 进位while (l1 != null || l2 != null || carry != 0) {int sum = carry; // 当前和初始化为进位值if (l1 != null) {sum += l1.val; // 加上l1的值l1 = l1.next; // 移动l1指针}if (l2 != null) {sum += l2.val; // 加上l2的值l2 = l2.next; // 移动l2指针}carry = sum / 10; // 计算新的进位current.next = new ListNode(sum % 10); // 创建新节点current = current.next; // 移动结果链表指针}return dummy.next; // 返回结果链表的头节点}
}
面试题 02.06. 回文链表
面试题 02.06. 回文链表
编写一个函数,检查输入的链表是否是回文的。
示例 1:
输入: 1->2 输出: false
示例 2:
输入: 1->2->2->1 输出: true
java">/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public boolean isPalindrome(ListNode head) {if (head == null) {return true;}List<Integer> list = new ArrayList<>();while (head != null) {list.add(head.val);head = head.next;}for (int i = 0, j = list.size() - 1; i < j; i++, j--) {if (!list.get(i).equals(list.get(j))) {return false;}}return true;}
}
面试题 02.07. 链表相交
面试题 02.07. 链表相交
给你两个单链表的头节点 headA
和 headB
,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null
。
图示两个链表在节点 c1
开始相交:
题目数据 保证 整个链式结构中不存在环。
注意,函数返回结果后,链表必须 保持其原始结构 。
示例 1:
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 输出:Intersected at '8' 解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。 从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。 在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
示例 2:
输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 输出:Intersected at '2' 解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。 从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。 在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
示例 3:
输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 输出:null 解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。 由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。 这两个链表不相交,因此返回 null 。
提示:
listA
中节点数目为m
listB
中节点数目为n
0 <= m, n <= 3 * 104
1 <= Node.val <= 105
0 <= skipA <= m
0 <= skipB <= n
- 如果
listA
和listB
没有交点,intersectVal
为0
- 如果
listA
和listB
有交点,intersectVal == listA[skipA + 1] == listB[skipB + 1]
java">public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {int lenA = 0;int lenB = 0;ListNode tempA = headA;ListNode tempB = headB;while (tempA != null) {lenA++;tempA = tempA.next;}while (tempB != null) {lenB++;tempB = tempB.next;}tempA = headA;tempB = headB;if (lenA > lenB) {for (int i = 0; i < lenA - lenB; i++) {tempA = tempA.next;}} else {for (int i = 0; i < lenB - lenA; i++) {tempB = tempB.next;}}while (tempA != null && tempB != null) {if (tempA == tempB) {return tempA;}tempA = tempA.next;tempB = tempB.next;}return null;}
}
近日总结:
好好学习!天天向上!
这两天突然降温,措不及防。
不过看天气预报未来几天都是在渐渐回暖的。
今天,也知道了一个词,倒春寒。
"倒春寒"是指在春季气温回升的过程中,突然出现的短暂寒冷天气现象。通常发生在春季中后期,本应逐渐变暖的时节,却因冷空气南下或天气系统变化,导致气温骤降,甚至出现霜冻、雨雪等寒冷天气。
和我妈视频通话,才知道家里还下了雪,不过我在学校,一直在室内,也不知道外面下了雪没有。
还有一件事情,CSDN居然更新了,从此以后我的题目再也不能靠小bug搞成<=4个字的了。
还有末尾的添加封面功能,居然也可以设置图片展示样式了。