0
点赞
收藏
分享

微信扫一扫

力扣第十八天

Star英 2022-02-03 阅读 59

文章目录

problem Ⅰ

160. Intersection of Two Linked Lists
Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

For example, the following two linked lists begin to intersect at node c1:

在这里插入图片描述

The test cases are generated such that there are no cycles anywhere in the entire linked structure.

Note that the linked lists must retain their original structure after the function returns.

Custom Judge:

The inputs to the judge are given as follows (your program is not given these inputs):

  • intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
  • listA - The first linked list.
  • listB - The second linked list.
  • skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
  • skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.

The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.

Example 1:
在这里插入图片描述

Example 2:
在这里插入图片描述

Example 3:
在这里插入图片描述

my solution Ⅰ

/**
 * 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) {
        if(!headA || !headB)return NULL;
        ListNode *a=headA, *b=headB;
        unordered_set<ListNode*> hashset;
        while(a || b){
            if(a){
                if(hashset.find(a) != hashset.end()){
                    return a;
                }else{
                    hashset.insert(a);
                    a = a->next;
                }
            }
            if(b){
                if(hashset.find(b) != hashset.end()){
                    return b;
                }else{
                    hashset.insert(b);
                    b = b->next;
                }
            }
        }
        return NULL;
    }
};

在这里插入图片描述
time complexity O ( n ) O(n) O(n)
space complexity O ( n ) O(n) O(n)

my solution Ⅱ

/**
 * 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) {
        ListNode *a=headA, *b=headB;
        while(a != b){
            a = !a ? headB : a->next;
            b = !b ? headA : b->next;
        }
        return a;
    }
};

time complexity O ( n ) O(n) O(n)
space complexity O ( 1 ) O(1) O(1)

NOTE:
在这里插入图片描述
在这里插入图片描述
an improve thought is to line up the ends of the two lists, so we can concatenate the two lists in opposite orders, like A+B and B+A, in that way we can use only O(1) extra space

problem Ⅱ

82. Remove Duplicates from Sorted List II
Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.

Example 1:
在这里插入图片描述

Example 2:
在这里插入图片描述

my solution

/**
 * 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* deleteDuplicates(ListNode* head) {
        ListNode * prev = new ListNode(), *curr = head;
        prev->next = head;
        head = prev;
        while(curr){
            if(curr->next && curr->next->val == curr->val){
                while(curr->next && curr->next->val == curr->val)
                    curr = curr->next;
                prev->next = curr->next;
                curr = curr->next;
            }else{
                curr = curr->next;
                prev = prev->next;
            }
        }
        return head->next;
    }
};

others solution slow than me

class Solution
{
public:
    ListNode* deleteDuplicates(ListNode* head)
    {
        if(!head)
            return head;
        
        ListNode *prev=NULL, *curr=head, *next=curr->next;
        while(next){
            if(curr->val == next->val){
                while(next && next->val == curr->val)
                    next=next->next;
                // This condition means element at the head is repeating. So, head pointer needs to be shifted.
                if(!prev)head=next;
                else prev->next = next;
            }
            else prev=curr;
            curr=next;
            if(next) next=curr->next;
        }
        return head;
    }
};
举报

相关推荐

0 条评论