diff options
author | 3gg <3gg@shellblade.net> | 2025-02-05 18:36:31 -0800 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2025-02-05 18:36:31 -0800 |
commit | 4689e4e80b479be25f7557d05818f5caa723aafa (patch) | |
tree | 4df25811fe2a9a15b401375178da6537f4b6063f /top-interview-questions/easy/linked_list |
Diffstat (limited to 'top-interview-questions/easy/linked_list')
7 files changed, 272 insertions, 0 deletions
diff --git a/top-interview-questions/easy/linked_list/01_delete_node_in_a_linked_list.cc b/top-interview-questions/easy/linked_list/01_delete_node_in_a_linked_list.cc new file mode 100644 index 0000000..47f9559 --- /dev/null +++ b/top-interview-questions/easy/linked_list/01_delete_node_in_a_linked_list.cc | |||
@@ -0,0 +1,22 @@ | |||
1 | /** | ||
2 | * Definition for singly-linked list. | ||
3 | * struct ListNode { | ||
4 | * int val; | ||
5 | * ListNode *next; | ||
6 | * ListNode(int x) : val(x), next(NULL) {} | ||
7 | * }; | ||
8 | */ | ||
9 | class Solution { | ||
10 | public: | ||
11 | void deleteNode(ListNode* node) { | ||
12 | while (node) { | ||
13 | if (node->next) { | ||
14 | node->val = node->next->val; | ||
15 | if (!node->next->next) { | ||
16 | node->next = NULL; | ||
17 | } | ||
18 | } | ||
19 | node = node->next; | ||
20 | } | ||
21 | } | ||
22 | }; | ||
diff --git a/top-interview-questions/easy/linked_list/02_remove_nth_node_from_end_of_list.cc b/top-interview-questions/easy/linked_list/02_remove_nth_node_from_end_of_list.cc new file mode 100644 index 0000000..6773f04 --- /dev/null +++ b/top-interview-questions/easy/linked_list/02_remove_nth_node_from_end_of_list.cc | |||
@@ -0,0 +1,32 @@ | |||
1 | /** | ||
2 | * Definition for singly-linked list. | ||
3 | * struct ListNode { | ||
4 | * int val; | ||
5 | * ListNode *next; | ||
6 | * ListNode() : val(0), next(nullptr) {} | ||
7 | * ListNode(int x) : val(x), next(nullptr) {} | ||
8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
9 | * }; | ||
10 | */ | ||
11 | class Solution { | ||
12 | public: | ||
13 | // Return the number of elements in the list. | ||
14 | int removeNth(ListNode* node, int n) { | ||
15 | if (!node) { | ||
16 | return 0; | ||
17 | } | ||
18 | const int tailLength = removeNth(node->next, n); | ||
19 | if (tailLength == n) { | ||
20 | node->next = node->next->next; | ||
21 | } | ||
22 | return tailLength + 1; | ||
23 | } | ||
24 | |||
25 | ListNode* removeNthFromEnd(ListNode* head, int n) { | ||
26 | const int listLength = removeNth(head, n); | ||
27 | if (listLength == n) { | ||
28 | head = head->next; | ||
29 | } | ||
30 | return head; | ||
31 | } | ||
32 | }; | ||
diff --git a/top-interview-questions/easy/linked_list/03_reverse_linked_list.cc b/top-interview-questions/easy/linked_list/03_reverse_linked_list.cc new file mode 100644 index 0000000..d97bd33 --- /dev/null +++ b/top-interview-questions/easy/linked_list/03_reverse_linked_list.cc | |||
@@ -0,0 +1,26 @@ | |||
1 | /** | ||
2 | * Definition for singly-linked list. | ||
3 | * struct ListNode { | ||
4 | * int val; | ||
5 | * ListNode *next; | ||
6 | * ListNode() : val(0), next(nullptr) {} | ||
7 | * ListNode(int x) : val(x), next(nullptr) {} | ||
8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
9 | * }; | ||
10 | */ | ||
11 | class Solution { | ||
12 | public: | ||
13 | // Reverse the list and return the head of the new list. | ||
14 | ListNode* reverse(ListNode* node) { | ||
15 | if (!node) { return nullptr; } | ||
16 | if (!node->next) { return node; } | ||
17 | ListNode* head = reverse(node->next); | ||
18 | node->next->next = node; | ||
19 | node->next = nullptr; | ||
20 | return head; | ||
21 | } | ||
22 | |||
23 | ListNode* reverseList(ListNode* head) { | ||
24 | return reverse(head); | ||
25 | } | ||
26 | }; | ||
diff --git a/top-interview-questions/easy/linked_list/04_merge_two_sorted_lists.cc b/top-interview-questions/easy/linked_list/04_merge_two_sorted_lists.cc new file mode 100644 index 0000000..9724ef0 --- /dev/null +++ b/top-interview-questions/easy/linked_list/04_merge_two_sorted_lists.cc | |||
@@ -0,0 +1,52 @@ | |||
1 | /** | ||
2 | * Definition for singly-linked list. | ||
3 | * struct ListNode { | ||
4 | * int val; | ||
5 | * ListNode *next; | ||
6 | * ListNode() : val(0), next(nullptr) {} | ||
7 | * ListNode(int x) : val(x), next(nullptr) {} | ||
8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
9 | * }; | ||
10 | */ | ||
11 | class Solution { | ||
12 | public: | ||
13 | ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { | ||
14 | ListNode* mergedHead = nullptr; | ||
15 | ListNode* merged = nullptr; | ||
16 | while (list1 && list2) { | ||
17 | if (list1->val <= list2->val) { | ||
18 | if (merged) { | ||
19 | merged->next = list1; | ||
20 | merged = merged->next; | ||
21 | } else { | ||
22 | merged = list1; | ||
23 | mergedHead = merged; | ||
24 | } | ||
25 | list1 = list1->next; | ||
26 | } else { | ||
27 | if (merged) { | ||
28 | merged->next = list2; | ||
29 | merged = merged->next; | ||
30 | } else { | ||
31 | merged = list2; | ||
32 | mergedHead = merged; | ||
33 | } | ||
34 | list2 = list2->next; | ||
35 | } | ||
36 | } | ||
37 | if (list1) { | ||
38 | if (merged) { | ||
39 | merged->next = list1; | ||
40 | } else { | ||
41 | mergedHead = list1; | ||
42 | } | ||
43 | } else if (list2) { | ||
44 | if (merged) { | ||
45 | merged->next = list2; | ||
46 | } else { | ||
47 | mergedHead = list2; | ||
48 | } | ||
49 | } | ||
50 | return mergedHead; | ||
51 | } | ||
52 | }; | ||
diff --git a/top-interview-questions/easy/linked_list/05_palindrome_linked_list.cc b/top-interview-questions/easy/linked_list/05_palindrome_linked_list.cc new file mode 100644 index 0000000..3f42e4a --- /dev/null +++ b/top-interview-questions/easy/linked_list/05_palindrome_linked_list.cc | |||
@@ -0,0 +1,48 @@ | |||
1 | /** | ||
2 | * Definition for singly-linked list. | ||
3 | * struct ListNode { | ||
4 | * int val; | ||
5 | * ListNode *next; | ||
6 | * ListNode() : val(0), next(nullptr) {} | ||
7 | * ListNode(int x) : val(x), next(nullptr) {} | ||
8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
9 | * }; | ||
10 | */ | ||
11 | class Solution { | ||
12 | public: | ||
13 | // Copy the list. | ||
14 | ListNode* clone(const ListNode* node) { | ||
15 | if (!node) return nullptr; | ||
16 | ListNode* copy = new ListNode(); | ||
17 | copy->val = node->val; | ||
18 | if (node->next) { | ||
19 | copy->next = clone(node->next); | ||
20 | } else { | ||
21 | copy->next = nullptr; | ||
22 | } | ||
23 | return copy; | ||
24 | } | ||
25 | |||
26 | // Reverse a list and return the new of the reversed list. | ||
27 | ListNode* reverse(ListNode* node) { | ||
28 | if (!node) return nullptr; | ||
29 | if (!node->next) return node; | ||
30 | ListNode* head = reverse(node->next); | ||
31 | node->next->next = node; | ||
32 | node->next = nullptr; | ||
33 | return head; | ||
34 | } | ||
35 | |||
36 | // Compare two lists for equality. | ||
37 | bool eq(const ListNode* a, const LideNode* b) { | ||
38 | if (!a && !b) return true; | ||
39 | if (!a) return false; | ||
40 | if (!b) return false; | ||
41 | if (a->val != b->val) return false; | ||
42 | return eq(a->next, b->next); | ||
43 | } | ||
44 | |||
45 | bool isPalindrome(ListNode* head) { | ||
46 | return eq(head, reverse(clone(head))); | ||
47 | } | ||
48 | }; | ||
diff --git a/top-interview-questions/easy/linked_list/05_palindrome_linked_list_2.cc b/top-interview-questions/easy/linked_list/05_palindrome_linked_list_2.cc new file mode 100644 index 0000000..590c485 --- /dev/null +++ b/top-interview-questions/easy/linked_list/05_palindrome_linked_list_2.cc | |||
@@ -0,0 +1,63 @@ | |||
1 | /** | ||
2 | * Definition for singly-linked list. | ||
3 | * struct ListNode { | ||
4 | * int val; | ||
5 | * ListNode *next; | ||
6 | * ListNode() : val(0), next(nullptr) {} | ||
7 | * ListNode(int x) : val(x), next(nullptr) {} | ||
8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
9 | * }; | ||
10 | */ | ||
11 | class Solution { | ||
12 | public: | ||
13 | // Return the length of the list. | ||
14 | size_t length(const ListNode* node) { | ||
15 | if (!node) return 0; | ||
16 | return 1 + length(node->next); | ||
17 | } | ||
18 | |||
19 | // Return the nth element of the list. | ||
20 | const ListNode* nth(const ListNode* node, size_t n) { | ||
21 | if (!node) return nullptr; | ||
22 | if (n == 0) return node; | ||
23 | return nth(node->next, n-1); | ||
24 | } | ||
25 | |||
26 | // Reverse the list up to, and not including, the nth element. | ||
27 | // Return the head of the reversed list (nth-1 node). | ||
28 | ListNode* reverse(ListNode* node, size_t n) { | ||
29 | if (!node) return nullptr; | ||
30 | if (!node->next) return node; | ||
31 | if (n == 1) { | ||
32 | node->next = nullptr; | ||
33 | return node; | ||
34 | } | ||
35 | |||
36 | ListNode* head = reverse(node->next, n-1); | ||
37 | node->next->next = node; | ||
38 | node->next = nullptr; | ||
39 | return head; | ||
40 | } | ||
41 | |||
42 | // Compare two lists for equality. | ||
43 | bool eq(const ListNode* a, const ListNode* b) { | ||
44 | if (!a && !b) return true; | ||
45 | if (!a) return false; | ||
46 | if (!b) return false; | ||
47 | if (a->val != b->val) return false; | ||
48 | return eq(a->next, b->next); | ||
49 | } | ||
50 | |||
51 | bool isPalindrome(ListNode* head) { | ||
52 | if (!head) return true; | ||
53 | if (!head->next) return true; | ||
54 | |||
55 | // If odd, get the middle element. | ||
56 | // If even, get the first element of the second half. | ||
57 | const size_t len = length(head); | ||
58 | const size_t middle = len/2; | ||
59 | const ListNode* head1 = nth(head, middle + (len & 1)); | ||
60 | const ListNode* head2 = reverse(head, middle); | ||
61 | return eq(head1, head2); | ||
62 | } | ||
63 | }; | ||
diff --git a/top-interview-questions/easy/linked_list/06_linked_list_cycle.cc b/top-interview-questions/easy/linked_list/06_linked_list_cycle.cc new file mode 100644 index 0000000..e8b4d1d --- /dev/null +++ b/top-interview-questions/easy/linked_list/06_linked_list_cycle.cc | |||
@@ -0,0 +1,29 @@ | |||
1 | /** | ||
2 | * Definition for singly-linked list. | ||
3 | * struct ListNode { | ||
4 | * int val; | ||
5 | * ListNode *next; | ||
6 | * ListNode(int x) : val(x), next(NULL) {} | ||
7 | * }; | ||
8 | */ | ||
9 | class Solution { | ||
10 | public: | ||
11 | bool hasCycle(ListNode *head) { | ||
12 | if (!head) return false; | ||
13 | if (!head->next) return false; | ||
14 | |||
15 | const ListNode* next1 = head; | ||
16 | const ListNode* next2 = head; | ||
17 | |||
18 | do { | ||
19 | next1 = next1->next; | ||
20 | next2 = next2->next; | ||
21 | if (next2) { | ||
22 | next2 = next2->next; | ||
23 | } | ||
24 | } | ||
25 | while ((next1 != head) && next1 && next2 && (next1 != next2)); | ||
26 | |||
27 | return next1 && next2 && (next1 == next2); | ||
28 | } | ||
29 | }; | ||