Journey of LeetCode|DAY 4
Preface
This is a new day to continue my LinkedList journey.
Learn something new and keep reviewing what I learnt before.
1.Swap Nodes in Pairs
LeetCode Link: 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.)
Analysis and Solution
Dummy Head Node
Define a Dummy Head to help the process.
Dummy Head points to head.
Define a current pointer, points to Dummy Head.
Save the 1ed node and 3rd node before current pointer change the point direction to 2nd node.
Let 2nd node points to 1ed node, and 1ed node points to 3rd node.
Swap the two nodes done.
Current pointer moves to the New second node and repeat the operation as before.
LeetCode C++ as followings Dummy Head 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* swapPairs(ListNode* head) {
ListNode* dummyHead = new ListNode(0);//define a new dummyHead to do the process easier
dummyHead->next = head;//dummyHead points to headNode
ListNode* cur = dummyHead;//difine a cur points to dummyHead
while(cur->next!=NULL&&cur->next->next!=NULL){//set boundary condition;cur->next=null if list is even nums;cur->next->next=null if list is odd nums,end loop
ListNode* temp = cur->next;//define a temp to save the cur->next
ListNode* temp2 = cur->next->next->next;//define a temp2 to save cur->next->next
cur->next = cur->next->next;//cur->next points to cur->next->next
cur->next->next = temp;//cur->next->next points to temp
cur->next->next->next = temp2;//cur->next->next->next points to temp2
cur = cur->next->next;//cur moves to next and next node
}
return dummyHead->next;//return the true headNode
}
};
2.Remove Nth Node From End of List
LeetCode Link: 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.
Analysis and Solution
Double Pointer
Define a dummy head and fast and slow pointer.
Let fast moves n steps at first, then one more.
To let the slow moves at the same time when fast moves to the end.
Here slow is pointing to the node before the node to be deleted.
Finally, just delete it is ok.
LeetCode C++ as followings Double Pointer
/**
* 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* dummyHead = new ListNode(0);//define a new dummyHead
dummyHead->next = head;//dummyHead points to head
ListNode* slow = dummyHead;//define a slow to point to dummyHead
ListNode* fast = dummyHead;//define a fast to point to dummyHead
while(n-- && fast != NULL) {//traverse fast,
fast = fast->next;//move fast.
}
fast = fast->next; // move fast one more,to let slow points to the node that before the node to be deleted
while (fast != NULL) {//traverse fast until end
fast = fast->next;//move fast
slow = slow->next;//move slow at the same time
}
slow->next = slow->next->next; //delete the node
// ListNode *tmp = slow->next; release memory
// slow->next = tmp->next;
// delete nth;
return dummyHead->next;
}
};
3.Linked List Cycle II
LeetCode Link: 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.
Analysis and Solution
Double Pointer
First, confirm if it is a cycle.
Define fast and slow, let fast moves 2 steps and slow moves 1 step, move them at the same time, they will meet if it is a cycle.
Second, find the inlet.
define an index1 to points to the meet point that fast = slow, and index2 to points to head, let them move 1 step at the same time, the inlet is where they meet each other.
LeetCode C++ as followings Double Pointer
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head;//define a fast to points to head
ListNode* slow = head;//define a slow to points to head
while(fast != NULL && fast->next != NULL) {//if it's not cycle,it will be end,
fast = fast->next->next;//fast moves 2 steps
slow = slow->next;//slow moves one step
// fast meets slow.define a new index1 to points to meetpoint ,a new index2 to points to head,and move them at the same time until they meet each other.
if (slow == fast) {//if it is cycle,slow will meet fast
ListNode* index1 = slow;//define a new index1 to points to meetpoint
ListNode* index2 = head;//define a new index2 to points to head
while (index1 != index2) {//traverse until index1 meets index2
index1 = index1->next;//index1 moves one step
index2 = index2->next;//index2 moves one step
}
return index1; //Here is the meetpoint of the index1 and index2,meantime is inlet
}
}
return NULL;//if it is not cycle
}
};
4. Intersection of Two Linked Lists
LeetCode Link: 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.
Analysis and Solution
Double Pointer
Define current A and current B
Get the length of A and B
Get the difference (gap) of the A and B
Move current A to where B starts
Move current A and current B at the same time, one step one time
If current A =current B, the intersection is where they meet
If current A cannot meet current B, return NULL, which means they have no intersection.
LeetCode C++ as followings Double Pointer
/**
* 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* curA = headA;//define a curA to points to headA
ListNode* curB = headB;//define a curB to points to headB
int lenA = 0, lenB = 0;//define the length of A and B
while (curA != NULL) { // traverse to get the Length of A
lenA++;//Length +1
curA = curA->next;//curA moves 1 step
}
while (curB != NULL) { // traverse to get the Length of B
lenB++;//Length +1
curB = curB->next;//curB moves 1 step
}
curA = headA;//let curA points to headA
curB = headB;//let curB points to headB
// Let curA points to the longest list,and lenA equals the length of longest list
if (lenB > lenA) {//change the A and B,if lenB is longer
swap (lenA, lenB);//swap the lenA and lenB
swap (curA, curB);//swap the curA and curB
}
// get the difference between the lenA and lenB
int gap = lenA - lenB;//get the gap
//let curA arrvies at the same location where curB points
while (gap--) {//move curA gap steps
curA = curA->next;
}
// traverse curA and curB,reture if curA=curB,which is intersection.
while (curA != NULL) {//traverse curA and curB
if (curA == curB) {//the intersection of the two list
return curA;//intersection point
}
curA = curA->next;//move one step
curB = curB->next;//move one step
}
return NULL;
}
};
Conclusion
No more rubbish to say here.
Just do it ,
And review what u have learnt.
Update the review situation in the future.