Skip to content

Commit c340c97

Browse files
committed
linked-list
1 parent c59c266 commit c340c97

7 files changed

+237
-24
lines changed

141-Linked-List-Cycle.c

-24
This file was deleted.

141-Linked-List-Cycle.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
ListNode *slow = head, *fast = head;
13+
while (nullptr != fast && nullptr != fast->next)
14+
{
15+
fast = fast->next->next;
16+
slow = slow->next;
17+
if (fast == slow)
18+
{
19+
return true;
20+
}
21+
}
22+
return false;
23+
}
24+
};

142-Linked-List-Cycle-II.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
ListNode *detectCycle(ListNode *head) {
12+
ListNode *slow = head, *fast = head;
13+
while (nullptr != fast && nullptr != fast->next)
14+
{
15+
fast = fast->next->next;
16+
slow = slow->next;
17+
if (fast == slow)
18+
{
19+
break;
20+
}
21+
}
22+
if (nullptr == fast || nullptr == fast->next)
23+
{
24+
return nullptr;
25+
}
26+
while (head != fast)
27+
{
28+
slow = fast;
29+
while (1)
30+
{
31+
slow = slow->next;
32+
if (slow == head)
33+
{
34+
return head;
35+
}
36+
if (slow == fast)
37+
{
38+
break;
39+
}
40+
}
41+
head = head->next;
42+
}
43+
return fast;
44+
}
45+
};

147-Insertion-Sort-List.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
ListNode* insertionSortList(ListNode* head) {
12+
if (nullptr == head || nullptr == head->next)
13+
{
14+
return head;
15+
}
16+
ListNode *lastNode = head;
17+
ListNode *insertNode, *pNode;
18+
while (nullptr != lastNode->next)
19+
{
20+
insertNode = lastNode->next;
21+
int tempVal = insertNode->val;
22+
if (tempVal >= lastNode->val)
23+
{
24+
lastNode = insertNode;
25+
continue;
26+
}
27+
lastNode->next = insertNode->next;
28+
if (tempVal <= head->val)
29+
{
30+
insertNode->next = head;
31+
head = insertNode;
32+
continue;
33+
}
34+
pNode = head;
35+
while (pNode->next != lastNode->next)
36+
{
37+
if (pNode->next->val >= tempVal)
38+
{
39+
insertNode->next = pNode->next;
40+
pNode->next = insertNode;
41+
break;
42+
}
43+
pNode = pNode->next;
44+
}
45+
}
46+
return head;
47+
}
48+
};
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
ListNode* deleteDuplicates(ListNode* head) {
12+
ListNode *pNode = head;
13+
while (nullptr != pNode && nullptr != pNode->next)
14+
{
15+
if (pNode->val == pNode->next->val)
16+
{
17+
pNode->next = pNode->next->next;
18+
}
19+
else
20+
{
21+
pNode = pNode->next;
22+
}
23+
}
24+
return head;
25+
}
26+
};

86-Partition-List.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
ListNode* partition(ListNode* head, int x) {
12+
ListNode *frontHead = nullptr, *frontTail, *backHead = nullptr, *backTail;
13+
while (nullptr != head)
14+
{
15+
if (head->val < x)
16+
{
17+
if (nullptr == frontHead)
18+
{
19+
frontHead = head;
20+
frontTail = head;
21+
}
22+
else
23+
{
24+
frontTail->next = head;
25+
frontTail = frontTail->next;
26+
}
27+
head = head->next;
28+
frontTail->next = nullptr;
29+
}
30+
else
31+
{
32+
if (nullptr == backHead)
33+
{
34+
backHead = head;
35+
backTail = head;
36+
}
37+
else
38+
{
39+
backTail->next = head;
40+
backTail = backTail->next;
41+
}
42+
head = head->next;
43+
backTail->next = nullptr;
44+
}
45+
}
46+
if (nullptr == frontHead)
47+
{
48+
return backHead;
49+
}
50+
frontTail->next = backHead;
51+
return frontHead;
52+
}
53+
};

92-Reverse-Linked-List-II.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
ListNode* reverseBetween(ListNode* head, int m, int n) {
12+
ListNode *tempNode, *pNode = head, *firstNode;
13+
if (1 == m)
14+
{
15+
firstNode = head;
16+
for (int i = 0; i < n - m; i++)
17+
{
18+
tempNode = firstNode->next;
19+
firstNode->next = tempNode->next;
20+
tempNode->next = head;
21+
head = tempNode;
22+
}
23+
}
24+
else
25+
{
26+
for (int i = 1; i < m - 1; i++)
27+
{
28+
pNode = pNode->next;
29+
}
30+
firstNode = pNode->next;
31+
for (int i = 0; i < n - m; i++)
32+
{
33+
tempNode = firstNode->next;
34+
firstNode->next = tempNode->next;
35+
tempNode->next = pNode->next;
36+
pNode->next = tempNode;
37+
}
38+
}
39+
return head;
40+
}
41+
};

0 commit comments

Comments
 (0)