代码随想录DAY4

24. Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)

Example 1:

Input: head = [1,2,3,4]
Output: [2,1,4,3]

Example 2:

Input: head = []
Output: []

Example 3:

Input: head = [1]
Output: [1]

这题看图去解,这种题目最好用到虚拟头结点,这样可以把真实的头结点当作同一种情况来处理。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode *DumpHead = new ListNode(0);
        DumpHead->next = head;
        ListNode *curr = DumpHead;
        while(curr->next!=NULL && curr->next->next!= NULL){
            ListNode *temp =curr->next;
            ListNode *temp1 = curr->next->next->next;
            curr->next = curr->next->next;
            curr->next->next = temp;
            curr->next->next->next =  temp1;
            curr = curr->next->next;
        }
            return DumpHead->next;
    }
};

19. Remove Nth Node From End of List

Given the head of a linked list, remove the nth node from the end of the list and return its head.

Example 1:

Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]

Example 2:

Input: head = [1], n = 1
Output: []

Example 3:

Input: head = [1,2], n = 1
Output: [1]

这题中,首先我也想到了用快慢指针。因为用N 如果快指针先走了n步,然后两边指针在一起走。最终的结果是,慢指针刚好停在了要删除的节点的前面。快指针到达node的尾部。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode * DumpHead = new ListNode(0);
        DumpHead->next = head;
        ListNode * fastpoint = DumpHead;
        ListNode * slowpoint = DumpHead;
        while(n--&&fastpoint->next != NULL){
            fastpoint= fastpoint->next;
        }
        while(fastpoint!=NULL &&fastpoint->next != NULL){
            fastpoint =fastpoint->next;
            slowpoint = slowpoint->next;
        }
        slowpoint->next = slowpoint->next->next; //不能直接用fastpoint是因为中间可以差了4到5个!
        return DumpHead->next;
    }
};
面试题 02.07. Intersection of Two Linked Lists LCCI

Given two (singly) linked lists, determine if the two lists intersect. Return the inter­ secting node. Note that the intersection is defined based on reference, not value. That is, if the kth node of the first linked list is the exact same node (by reference) as the jth node of the second linked list, then they are intersecting.

Example 1:

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output: Reference of the node with value = 8
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
Example 2:

Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Reference of the node with value = 2
Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
Example 3:

Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: null
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int lenA = 0 ;
        int lenB = 0 ;
        ListNode *currA = headA;
        ListNode *currB = headB;
        while(currA!=NULL){
            lenA++;
            currA = currA->next;
        }
        while(currB!=NULL){
            lenB++;
            currB = currB->next;
        }
        currA = headA;
        currB = headB;
        if(lenA <lenB){
            swap(lenA,lenB);
            swap(currA,currB);
        }
        int gap = lenA - lenB;
         while (gap--) {
            currA = currA->next;
        }
        while(currA!=NULL){
            if(currA ==currB){
                return currA;
            }
            currA =currA->next;
            currB = currB->next;
        }
        return NULL;
    }
};
142. Linked List Cycle II

难度中等

2149

Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.

Do not modify the linked list.

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.

这一题比较简单,只需要注意快慢指针的分布就可以了

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int lenA = 0 ;
        int lenB = 0 ;
        ListNode *currA = headA;
        ListNode *currB = headB;
        while(currA!=NULL){
            lenA++;
            currA = currA->next;
        }
        while(currB!=NULL){
            lenB++;
            currB = currB->next;
        }
        currA = headA;
        currB = headB;
        if(lenA <lenB){
            swap(lenA,lenB);
            swap(currA,currB);
        }
        int gap = lenA - lenB;
         while (gap--) {
            currA = currA->next;
        }
        while(currA!=NULL){
            if(currA ==currB){
                return currA;
            }
            currA =currA->next;
            currB = currB->next;
        }
        return NULL;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值