Skip to content

Commit f5708d8

Browse files
committed
🚀 23-Aug-2020
1 parent dffb20a commit f5708d8

7 files changed

+572
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
Given a linked list, determine if it has a cycle in it.
2+
3+
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
4+
5+
6+
7+
Example 1:
8+
9+
Input: head = [3,2,0,-4], pos = 1
10+
Output: true
11+
Explanation: There is a cycle in the linked list, where tail connects to the second node.
12+
13+
14+
Example 2:
15+
16+
Input: head = [1,2], pos = 0
17+
Output: true
18+
Explanation: There is a cycle in the linked list, where tail connects to the first node.
19+
20+
21+
Example 3:
22+
23+
Input: head = [1], pos = -1
24+
Output: false
25+
Explanation: There is no cycle in the linked list.
26+
27+
28+
29+
30+
Follow up:
31+
32+
Can you solve it using O(1) (i.e. constant) memory?
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
/**
45+
* Definition for singly-linked list.
46+
* struct ListNode {
47+
* int val;
48+
* ListNode *next;
49+
* ListNode(int x) : val(x), next(NULL) {}
50+
* };
51+
*/
52+
class Solution {
53+
public:
54+
bool hasCycle(ListNode *head) {
55+
if(!head) return false;
56+
ListNode *slow=head, *fast=head;
57+
while(fast!=NULL && fast->next!=NULL){
58+
slow=slow->next;
59+
fast=fast->next->next;
60+
if(slow==fast) return true;
61+
}
62+
63+
return false; // no cycle found
64+
}
65+
};
66+
67+
68+
69+
// Alter: Can also use hashing approach
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
2+
3+
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
4+
5+
Note: Do not modify the linked list.
6+
7+
8+
9+
Example 1:
10+
11+
Input: head = [3,2,0,-4], pos = 1
12+
Output: tail connects to node index 1
13+
Explanation: There is a cycle in the linked list, where tail connects to the second node.
14+
15+
16+
Example 2:
17+
18+
Input: head = [1,2], pos = 0
19+
Output: tail connects to node index 0
20+
Explanation: There is a cycle in the linked list, where tail connects to the first node.
21+
22+
23+
Example 3:
24+
25+
Input: head = [1], pos = -1
26+
Output: no cycle
27+
Explanation: There is no cycle in the linked list.
28+
29+
30+
31+
32+
Follow-up:
33+
Can you solve it without using extra space?
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
/**
46+
* Definition for singly-linked list.
47+
* struct ListNode {
48+
* int val;
49+
* ListNode *next;
50+
* ListNode(int x) : val(x), next(NULL) {}
51+
* };
52+
*/
53+
class Solution {
54+
public:
55+
ListNode *detectCycle(ListNode *head) {
56+
if(!head) return NULL;
57+
if(head->next==NULL) return NULL;
58+
ListNode *slow=head, *fast=head;
59+
bool loop=false;
60+
while(fast!=NULL && fast->next!=NULL){
61+
slow=slow->next;
62+
fast=fast->next->next;
63+
if(slow==fast){
64+
loop=true;
65+
break;
66+
}
67+
}
68+
if(!loop) return NULL; // no cycle
69+
slow=head;
70+
while(slow!=fast && fast!=NULL){
71+
slow=slow->next;
72+
fast=fast->next;
73+
}
74+
return slow;
75+
}
76+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
Write a program to find the node at which the intersection of two singly linked lists begins.
2+
3+
For example, the following two linked lists:
4+
5+
6+
begin to intersect at node c1.
7+
8+
9+
10+
Example 1:
11+
12+
13+
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
14+
Output: Reference of the node with value = 8
15+
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,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
16+
17+
18+
Example 2:
19+
20+
21+
Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
22+
Output: Reference of the node with value = 2
23+
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 [1,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.
24+
25+
26+
Example 3:
27+
28+
29+
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
30+
Output: null
31+
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.
32+
Explanation: The two lists do not intersect, so return null.
33+
34+
35+
Notes:
36+
37+
If the two linked lists have no intersection at all, return null.
38+
The linked lists must retain their original structure after the function returns.
39+
You may assume there are no cycles anywhere in the entire linked structure.
40+
Each value on each linked list is in the range [1, 10^9].
41+
Your code should preferably run in O(n) time and use only O(1) memory.
42+
43+
44+
45+
46+
47+
48+
49+
50+
// counting differences
51+
/**
52+
* Definition for singly-linked list.
53+
* struct ListNode {
54+
* int val;
55+
* ListNode *next;
56+
* ListNode(int x) : val(x), next(NULL) {}
57+
* };
58+
*/
59+
class Solution {
60+
public:
61+
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
62+
if(!headA || !headB) return NULL;
63+
64+
int lenA=0, lenB=0;
65+
ListNode *curr=headA;
66+
while(curr!=NULL){
67+
lenA++;
68+
curr=curr->next;
69+
}
70+
curr=headB;
71+
while(curr!=NULL){
72+
lenB++;
73+
curr=curr->next;
74+
}
75+
ListNode *curA=headA, *curB=headB;
76+
if(lenA > lenB){
77+
// move pointer of A to match length of B
78+
int forward=lenA-lenB;
79+
while(forward--){
80+
curA=curA->next;
81+
}
82+
} else if(lenB > lenA){
83+
// move pointer of B to match length of A
84+
int forward=lenB-lenA;
85+
while(forward--){
86+
curB=curB->next;
87+
}
88+
}
89+
while(curA && curB){
90+
if(curA==curB) return curA;
91+
curA=curA->next;
92+
curB=curB->next;
93+
}
94+
return NULL;
95+
}
96+
};
97+
98+
99+
/*
100+
Approach 1: Brute Force
101+
102+
Approach 2: Hash Table
103+
104+
Approach 3: Two Pointers
105+
Maintain two pointers pA and pB initialized at the head of A and B, respectively.
106+
Then let them both traverse through the lists, one node at a time.
107+
When pApA reaches the end of a list, then redirect it to the head of B (yes, B, that's right.);
108+
Similarly when pB pB reaches the end of a list, redirect it the head of A.
109+
If at any point pA meets pB, then pA/pB is the intersection node.
110+
111+
Approach 4: Implemented above
112+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Given a singly linked list, determine if it is a palindrome.
2+
3+
Example 1:
4+
5+
Input: 1->2
6+
Output: false
7+
Example 2:
8+
9+
Input: 1->2->2->1
10+
Output: true
11+
Follow up:
12+
Could you do it in O(n) time and O(1) space?
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
/**
25+
* Definition for singly-linked list.
26+
* struct ListNode {
27+
* int val;
28+
* ListNode *next;
29+
* ListNode() : val(0), next(nullptr) {}
30+
* ListNode(int x) : val(x), next(nullptr) {}
31+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
32+
* };
33+
*/
34+
class Solution {
35+
public:
36+
37+
38+
ListNode* getMiddle(ListNode* head){
39+
ListNode *slow=head, *fast=head;
40+
while(fast!=NULL && fast->next!=NULL){
41+
slow=slow->next;
42+
fast=fast->next->next;
43+
}
44+
return slow;
45+
};
46+
47+
ListNode* reverseList(ListNode* head){
48+
if(head==NULL) return head;
49+
ListNode *cur=head, *prev=head, *ahead=head->next;
50+
prev->next=NULL;
51+
cur=ahead;
52+
while(ahead!=NULL){
53+
ahead=ahead->next;
54+
cur->next=prev;
55+
prev=cur;
56+
cur=ahead;
57+
}
58+
head=prev;
59+
return head;
60+
}
61+
62+
bool isPalindrome(ListNode* head) {
63+
if(!head) return true;
64+
ListNode* mid=getMiddle(head);
65+
ListNode* last=reverseList(mid); // start of reverses list
66+
67+
while(head!=mid){
68+
if(head->val!=last->val) return false;
69+
head=head->next;
70+
last=last->next;
71+
}
72+
return true;
73+
74+
}
75+
};
76+
77+
// After finding the result, we can again reverse the reversed list and join it back to the original list

0 commit comments

Comments
 (0)