leetcode brush record day033:203 and 160

203. Simple difficulty: Method 1: Original: new linked list Principle: first, create two new nodes, one as the answer ch...

203. Simple difficulty:

Method 1: Original: new linked list

Principle: first, create two new nodes, one as the answer chain list and the other as the new chain header node of the return value.
Start to cycle through the question transfer linked list. As long as the current node of the question transfer linked list is not empty, it will cycle all the time.
If the value of the current node is not the value of the title pass val, the current node is regarded as an item of the new linked list now.next = head;, At the same time, move now and head backward. now = now.next head = head.next;
If the value of the current node is question pass val, move head back directly and now does not move.
After the loop ends, we have a vulnerability. If the value of the last node in the question transfer linked list is question transfer val, our now will not execute now.next = head; However, during the operation of the penultimate node, because our now executes now.next = head; So now.next.next is the last node, which will lead to the following situations:
The title is passed in [1,2,6,3,4,5,6] and we return [1,2,3,4,5,6]
Therefore, after the loop is completed, we also need to judge that if now.next exists and its val is the title passing val, we need to execute now.next = null; Remove the last node.

class Solution { public ListNode removeElements(ListNode head, int val) { ListNode now = new ListNode(); // To build a new linked list ListNode ans = now; // As return value while(head != null){ if(head.val != val){ now.next = head; // Move now backward only when the current node can be added now = now.next; } head = head.next; } // To prevent the input [1,2,6,3,4,5,6] from returning to [1,2,3,4,5,6] if(now.next != null && now.next.val == val){ now.next = null; } return ans.next; } }
Method 2: iteration:

Principle:

class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummyHead = new ListNode(0); dummyHead.next = head; ListNode temp = dummyHead; while (temp.next != null) { if (temp.next.val == val) { temp.next = temp.next.next; } else { temp = temp.next; } } return dummyHead.next; } }

160. Simple difficulty:

Requirement: after the function returns the result, the linked list must maintain its original structure

Method 1: original violence hash table:

Idea: create A hash table in which each element is A node. Then we traverse one of the linked lists A and B, store all its nodes, and then traverse the other linked list. Every time we get A node, we judge whether it exists in the hash table. If it exists, we return the node, otherwise we return null

public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { Set<ListNode> visited = new HashSet<ListNode>(); while(headA != null){ visited.add(headA); headA = headA.next; } while(headB != null){ if(visited.contains(headB)){ return headB; } headB = headB.next; } return null; } }
Method 2: double pointer:

Principle: according to the meaning of the topic, if two linked lists intersect, the length after the intersection point is the same.
To this end, we let the two linked lists traverse from the same distance at the end of the same distance. This position can only be the head node position of the short linked list.

Graphic process: image source: https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/tu-jie-xiang-jiao-lian-biao-by-user7208t/

Process Description: A and B may not be the same length, so we need to make the two pointers traversing the two linked lists at the same distance before the same end.
As an example in the above figure, B is shorter than A. let's first traverse the two pointers backward from their respective heads, B to the end, and then point to the end of A. at this time, the spacing between pointers pa and pb is the length of the B linked list.
We need to take advantage of the fact that the distance between the two pointers is B long. At this time, pa and pb continue to move back, and the period distance is always b long. When pa reaches the end, it points to head B. at this time, the position of pb in chain A is B long from the end, and pa is B long from the end at the head of chain B.
That is, we want to create a scenario where the difference between pa and pb is a short chain length.
Then traverse backward at the same time. If it meets, it returns. If it does not meet, it returns null

public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) return null; ListNode pA = headA, pB = headB; while (pA != pB) { pA = pA == null ? headB : pA.next; pB = pB == null ? headA : pB.next; } return pA; } }

26 October 2021, 03:49 | Views: 7224

Add new comment

For adding a comment, please log in
or create account

0 comments