-
Notifications
You must be signed in to change notification settings - Fork 0
/
15-04-2024
40 lines (37 loc) · 859 Bytes
/
15-04-2024
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
class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode* slow = head;
ListNode* fast = head;
while(fast != NULL && fast->next != NULL){
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
};
class Solution {
public:
void reverse(ListNode* head, ListNode*& last){
if(head == NULL){
return ;
}
reverse(head->next, last);
if(head and !head->next){
last = head;
}
else{
head->next->next = head;
}
}
ListNode* reverseList(ListNode* head) {
ListNode* cur = head;
ListNode* lastNode = head;
reverse(head, lastNode);
if(head){
head->next = NULL;
}
head = lastNode;
return head;
}
};