-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoPtrListt.c
132 lines (101 loc) · 2.6 KB
/
TwoPtrListt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
#include <stdio.h>
#include <stdbool.h>
/*
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
第一种 遍历方式, 可以把头结点判断放到循环
0 1 2 3 4 5 6 7 8 9 0
0 2 4 6 8 0 2 4 6 8 0
第二种遍历方式,快起一步
0 1 2 3 4 5 6 7 8 9 0
1 3 5 7 9 1 3 5 7 9
*/
struct ListNode{
int val;
struct ListNode *next;
};
//环形链表1
// 判断是否有 环连表 可以把头的判断直接放到循环里
bool hasCycle(struct ListNode *head) {
struct ListNode * slow = head;
struct ListNode * fast = head;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
return true;
}
return false;
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
// 环形链表2
// 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode *slow = head;
struct ListNode *fast = head;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
break;
}
if( fast == NULL || fast->next == NULL)
return NULL;
while(head && slow){
if(slow == head)
return head;
slow = slow->next;
head = head->next;
}
return NULL;
}
//找到两个单链表相交的起始节点。
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
if(!headA || !headB)
return NULL;
struct ListNode *nodeA = headA;
struct ListNode *nodeB = headB;
int flagA = true;
int flagB = true;
/* find A - B len */
while(nodeA && nodeB ){
if(nodeA == nodeB )
return nodeA;
nodeA = nodeA->next;
if(flagA && nodeA == NULL){
nodeA = headB;
flagA = false;
}
nodeB = nodeB->next;
if(flagB && nodeB == NULL){
nodeB = headA;
flagB = false;
}
}
return NULL;
}
//给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
struct ListNode *fast = head;
struct ListNode **slow = &head;
while(n--)
fast = fast->next;
while(fast){
fast = fast->next;
slow = &(*slow)->next;
}
*slow = (*slow)->next;
return head;
}