Day4 链表 part2
Leetcode24 Swap Nodes in Pairs
Method1
找到2个临时ListNode, 按顺序换
class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null) return null;
ListNode dummy = new ListNode(-1), p = dummy;
dummy.next = head;
while (p.next != null && p.next.next != null){
ListNode tmp = p.next, tmp2 = p.next.next.next;
p.next = p.next.next;
p.next.next = tmp;
p.next.next.next = tmp2;
p = p.next.next;
}
return dummy.next;
}
}
Method2 递归
可以看成后序遍历, 需要返回newNode为后面所用
class Solution {
public ListNode swapPairs(ListNode head) {
// base case
if (head == null || head.next == null) return head;
ListNode next = head.next;
ListNode newNode = swapPairs(next.next);
next.next = head;
head.next = newNode;
return next;
}
}
Leetcode19 Remove Nth Node From End of List
题目链接
双指针法, fast先移到第n位, 再fast/slow同时移
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head == null || head.next == null) return null;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode slow = dummy;
ListNode fast = dummy;
for (int i = 0; i < n; i++){
fast = fast.next;
}
while (fast.next != null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}
Leetcode160 Intersection of Two Linked Lists
Method1 HashSet
用HashSet记录判断是否为同一地址的ListNode
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
HashSet<ListNode> set = new HashSet<>();
while (headA != null){
set.add(headA);
headA = headA.next;
}
while (headB != null){
if (set.contains(headB)) return headB;
headB = headB.next;
}
return null;
}
}
Method2 双指针
求出两个链表的长度,并求出两个链表长度的差值,然后让curA移动到,和curB 对齐的位置
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA = headA;
ListNode curB = headB;
int lenA = 0, lenB = 0;
while (curA != null) { // 求链表A的长度
lenA++;
curA = curA.next;
}
while (curB != null) { // 求链表B的长度
lenB++;
curB = curB.next;
}
curA = headA;
curB = headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
//1. swap (lenA, lenB);
int tmpLen = lenA;
lenA = lenB;
lenB = tmpLen;
//2. swap (curA, curB);
ListNode tmpNode = curA;
curA = curB;
curB = tmpNode;
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上(末尾位置对齐)
while (gap-- > 0) {
curA = curA.next;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != null) {
if (curA == curB) {
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
}
}
Leetcode142 Linked List Cycle II
Method1 HashSet
time complexity: O(n)
space complexity: O(n) -> need to store all nodes
public class Solution {
public ListNode detectCycle(ListNode head) {
HashSet<ListNode> set = new HashSet<>();
ListNode result = new ListNode(-1);
while (head != null){
if (set.contains(head)){
result = head;
return result;
}
set.add(head);
head = head.next;
}
return null;
}
}
Method2 龟兔赛跑
- 先判断有无环
- 再判断环入口
time complexity: O(n)
space complexity: O(1)
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if (slow == fast){//有环
ListNode tmp1 = head, tmp2 = fast;
while (tmp1 != tmp2){
tmp1 = tmp1.next;
tmp2 = tmp2.next;
}
return tmp2;
}
}
return null;
}
}