-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2-3.删除链表中间结点和a除以b位置的结点.cpp
55 lines (47 loc) · 1.06 KB
/
2-3.删除链表中间结点和a除以b位置的结点.cpp
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
typedef struct Node {
int value;
Node* next;
Node( int data ) {
this.value = data;
}
} Node;
Node* removeMidNode( Node* head ) {
if( head == NULL || head->next == NULL ) {
return head;
}
if( head->next->next == NULL ) {
return head->next;
}
Node* pre = head;
Node* cur = head->next->next;
while( cur->next != NULL && cur->next->next != NULL ) {
pre = pre->next;
cur = cur->next->next;
}
Node* mid = pre->next;
pre->next = pre->next->next;
delete mid;
mid = NULL;
return head;
}
Node* removeByRatio( Node* head, int a, int b ) {
if( a < 1 || a > b ) return head;
int len = 0;
Node* cur = head;
while( cur != NULL ) {
len++;
cur = cur->next;
}
int pos = (int)ceil( ((double)( a * n ) ) / (double)b );
if( pos == 1 ) {
head = head->next;
}
else if( pos > 1 ) {
cur = head;
while( --pos != 1 ) {
cur = cur->next;
}
cur->next = cur->next->next;
}
return head;
}