141. 环形链表
给定一个链表,判断链表中是否有环。
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
示例 1:
输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:
输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。
示例 3:
输入:head = [1], pos = -1
输出:false
解释:链表中没有环。
进阶:
你能用 O(1)(即,常量)内存解决此问题吗?
// 141. 2, 双指针法:使用一个快指针一个慢指针一起遍历,如果是环形链表,那么他们迟早会相遇
private boolean hasCycle2_141(ListNode head) {
if (head == null || head.next == null) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
2. 链表的中间结点
给定一个带有头结点 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
示例 1:
输入:[1,2,3,4,5]
输出:此列表中的结点 3 (序列化形式:[3,4,5])
返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
注意,我们返回了一个 ListNode 类型的对象 ans,这样:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.
示例 2:
输入:[1,2,3,4,5,6]
输出:此列表中的结点 4 (序列化形式:[4,5,6])
由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。
//双指针:一个slow = next,一个fast = next.next,当fast遍历结束时,slow正好在中间
private ListNode middleNode3_876(ListNode head) {
ListNode slow = head;
if (head == null) {
return null;
} else {
ListNode fast = head.next;
while (fast != null) {
slow = slow.next;
fast = fast.next;
if (fast == null) {
return slow;
} else {
fast = fast.next;
}
}
}
return slow;
}
static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
3. 删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
示例:
给定一个链表: 1->2->3->4->5, 和 n = 2.
当删除了倒数第二个节点后,链表变为 1->2->3->5.
说明:
给定的 n 保证是有效的。
进阶:
你能尝试使用一趟扫描实现吗?
//2. 双指针: 第一个指针先移动到第n个元素,然后第二个指针一起移动,
// 当第一个指针移动到尾结点时,说明第一个指针就是倒数第n个节点 ok
private static ListNode myremoveNthFromEnd3_19(ListNode head, int n) {
if (head == null) {
return null;
}
ListNode fast = null;
ListNode slow = null;
while (n > 0) {
if (fast == null) {
fast = head;
} else {
fast = fast.next;
}
//n大于链表长度
if (fast == null) {
return head;
}
n--;
}
while (fast != null) {
if (fast.next == null) {
//说明删除的是第一个节点
if (slow == null) {
return head.next;
}
//删除节点
ListNode next = slow.next;
slow.next = next.next;
return head;
} else {
//更新slow和fast
if (slow == null) {
slow = head;
} else {
slow = slow.next;
}
fast = fast.next;
}
}
//到这说明n<=0
return head;
}
static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
4. 验证回文串
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
输入: “A man, a plan, a canal: Panama”
输出: true
示例 2:
输入: “race a car”
输出: false
public boolean isPalindrome(String s) {
String str = s.toUpperCase();
int left = 0;
int right = str.length() - 1;
while (left < right) {
char cleft = str.charAt(left);
char cright = str.charAt(right);
while (left < right) {
if ((cleft <= 'Z' && cleft >= 'A') || (cleft <= 'z' && cleft >= 'a') || (cleft <= '9' && cleft >='0')) {
break;
} else {
left++;
cleft = str.charAt(left);
}
}
while (left < right) {
if ((cright <= 'Z' && cright >= 'A') || (cright <= 'z' && cright >= 'a') || (cright <= '9' && cright >='0')) {
break;
} else {
right--;
cright = str.charAt(right);
}
}
if (cleft - cright != 0) {
return false;
}
right--;
left++;
}
return true;
}