-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBDLinkList.cpp
77 lines (70 loc) · 1.97 KB
/
BDLinkList.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// bytedance
#include <iostream>
using namespace std;
struct ListNode{
int val;
ListNode* next;
ListNode(int x):val(x), next(nullptr){};
};
// 反转指定链表
ListNode* reverse(ListNode* head){
if(head == nullptr) return nullptr;
ListNode* prev = nullptr;
ListNode* current = head;
ListNode* tmp;
while(current->next != nullptr){
tmp = current->next;
current->next = prev;
prev = current;
current = tmp;
}
current->next = prev;
return current;
}
// 反转链表最后 n 个元素
ListNode* reverseList(ListNode* head, int n){
int len = 0;
ListNode* current = head;
// 求出链表长度
while(current != nullptr){
len++;
current = current->next;
}
// 如果 n 等于链表长度,此时需要反转整个链表,如果 n > len 这种情况目前没有定义行为,出于完整性考虑,应该增加该部分的处理
if(n == len){
return reverse(head);
}
// 找到反转的位置,然后将反转的位置的前一个位置,然后将后续节点调用 reverse 反转
int position = 0;
current = head;
ListNode* dummy;
ListNode* prev;
while(current != nullptr){
if(position == len - n - 1){
prev = current;
dummy = reverse(prev->next);
break;
}
position++;
current = current->next;
}
// 最后将返回的dummy 与上述找到的位置拼接即可
prev->next = dummy;
return head;
}
int main() {
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(3);
head->next->next->next = new ListNode(4);
head->next->next->next->next = new ListNode(5);
head->next->next->next->next->next = new ListNode(6);
ListNode* dummy = reverseList(head, 6);
ListNode* current = dummy;
while(current){
cout << current->val << " ";
current = current->next;
}
cout << endl;
return 0;
}