DEV Community

Flame Chan
Flame Chan

Posted on

LeetCode Day4 LinkedList part2

LeetCode No.19 Remove Nth element 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.
Original Page

This is the 1st version of the code with 2 problems.

Image description

    public ListNode removeNthFromEnd(ListNode head, int n) {
        int size = 0;
        ListNode cur = head;
        while(cur!=null){
            size++;
            cur=cur.next;
        }
        int target = size - n + 1;
        cur = head;
        ListNode dummy = new ListNode(-1,head);
        ListNode pre = dummy;
        for(int i=0; i<target-1; i++){
            pre = cur;
            cur = cur.next;
        }
        pre.next = cur.next;
        return dummy.next;
    }
Enter fullscreen mode Exit fullscreen mode

it seams like we can do it in only 1 loop
Nth from the end of the list so we can use double vector, the spacing between these two vectors is N

  • when the fast vector finishes, the slow vector still has N steps, so the element here is the Nth from the end of the list!

Image description

LeetCode CN 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.

Image description

Example 1:

Image description

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:

Image description

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.
Notes:

If the two linked lists have no intersection at all, return null.
The linked lists must retain their original structure after the function returns.
You may assume there are no cycles anywhere in the entire linked structure.
Your code should preferably run in O(n) time and use only O(1) memory.

        int sizeA = 0;
        int sizeB = 0;
        ListNode curA = headA;
        ListNode curB = headB;
        //find the size of both list A and list B
        while(curA!= null){
            sizeA++;
            curA = curA.next;
        }
        while(curB!= null){
            sizeB++;
            curB = curB.next;
        }
        // remove the aboslutely redundant elements (the extra element definitly not the overlap parts) 
        int size = Math.abs(sizeA-sizeB);
        curA = headA;
        curB = headB;
        if(sizeA>sizeB){
            for(int i=0; i<size; i++){
                curA = curA.next;
            }
        }else{

            for(int i=0; i<size; i++){
                curB = curB.next;
            }
        }
        //Because if they overlap, they will link to the same Node (address, reference is the same)
        while(curA!=null && curB!=null){
            if(curA == curB){
                return curA;
            }else{
                curA = curA.next;
                curB = curB.next;
            }
        }
    return null;
    }
Enter fullscreen mode Exit fullscreen mode

142. Linked List Cycle II

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.
Original Page

Example 1:

Image description

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.

Constraints:

The number of the nodes in the list is in the range [0, 104].
-105 <= Node.val <= 105
pos is -1 or a valid index in the linked-list.

  • Seems like we can use Java Collection and here the Set would be fine, because we want to find the entry of the linked list cycle, which imply if the node reference has been added to the set, it would be the entry.
    public ListNode detectCycle(ListNode head) {
        HashSet<ListNode> dataSet = new HashSet<ListNode>();
        ListNode cur = head;
        int pos = 0;
        while(cur!=null){
            if(dataSet.contains(cur)){
                return cur;
            }
            dataSet.add(cur);
            cur = cur.next;
            pos++;
        }

        return null;
    }
Enter fullscreen mode Exit fullscreen mode

I found a new thought for this question, but now it is a little bit hard for me to understand (I mean I know why using this thought to figure out this question but if I suffer some other scenario I do not think I can come up with a method that has this kind of mathematical thought)

Image description

Image description

Top comments (0)