代码随想录刷题-day04

代码随想录刷题day04

  • 今日内容
    • 两两交换链表中的节点
      • 解题思路
      • 实现
    • 删除链表的倒数第N个结点
      • 解题思路
      • 实现
    • 链表相交
      • 解题思路
      • 实现
    • 环形链表II
      • 解题思路
      • 实现

今日内容

  1. 两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题 02.07. 链表相交 142.环形链表II

两两交换链表中的节点

解题思路

  1. cur指向虚头节点(上一轮的最后节点),保存cur->next和下轮起始节点
  2. cur->next = 2;2->next = 1;1->next = next

实现

Java

class Solution {
    public ListNode swapPairs(ListNode head) {
        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;
    }
}

C++

public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* dummy = new ListNode(0,head);
        ListNode* cur = dummy;
        while(cur->next != nullptr && cur->next->next != nullptr){
            ListNode* next = cur->next->next->next;
            ListNode* tmp = cur->next;

            cur->next = cur->next->next;
            cur->next->next = tmp;
            tmp->next = next;

            cur = cur->next->next;
        }
        return dummy->next;
    }
};

删除链表的倒数第N个结点

解题思路

  1. 使用一个虚头节点,使用双指针,快指针先走n,然后快慢一起走,慢指针指向被删节点的前节点

实现

Java

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode pre = new ListNode(0,head);
        ListNode start = pre ,end = pre;
        while(n!=0){
            start = start.next;
            n--;
        }
        while(start.next!=null){
            start = start.next;
            end = end.next;
        }
        end.next = end.next.next;
        return pre.next;
    }
}

C++

public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummy = new ListNode(0,head);
        ListNode* fast = dummy;
        ListNode* slow = dummy;
        while(n-- && fast != nullptr){
            fast = fast->next;
        }
        while(fast->next != nullptr){
            fast = fast->next;
            slow = slow->next;
        }
        slow->next = slow->next->next;
        return dummy->next;
    }
};

链表相交

解题思路

  1. 两个链表一个遍历完了转向另一个,两个链表指向同一个元素,就是相交节点

实现

Java

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode A = headA;
        ListNode B = headB;
        while(A!=B){
            A = A == null ? headB : A.next;
            B = B == null ? headA : B.next; 
        }
        return A;
    }
}

C++

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* A = headA;
        ListNode* B = headB;
        while(A!=B){
            A = A==NULL ? headB : A->next;
            B = B==NULL ? headA : B->next;
        }
        return A;
    }
};

环形链表II

解题思路

  1. 双指针的方法,起点都是head,快指针走两步,慢指针走一步,快慢指针相遇表示是环形链表
  2. 快指针留在原位,慢指针回到head,两指针每次走一步,相遇节点表示是环的起始点。

实现

Java

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

        while(fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                ListNode index1 = fast;
                ListNode index2 = head;
                while(index1!=index2){
                    index1=index1.next;
                    index2=index2.next;
                }
                return index1;
            }
        }
        return null;
  	}
}

C++

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode* fast = head;
        ListNode* slow = head;
        while(fast != NULL && fast->next != NULL){
            fast = fast->next->next;
            slow = slow->next;
            if(fast==slow){
                ListNode* index1 = fast;
                ListNode* index2 = head;
                while(index1!=index2){
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index2;
            }
        }
        return NULL;
    }
};

你可能感兴趣的:(Leetcode,链表,数据结构,leetcode)