-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathReverseList.h
More file actions
130 lines (117 loc) · 3.44 KB
/
ReverseList.h
File metadata and controls
130 lines (117 loc) · 3.44 KB
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
//
// ReverseList.h
// linkedList
//
// Created by junl on 2019/8/2.
// Copyright © 2019 junl. All rights reserved.
//
#ifndef ReverseList_hpp
#define ReverseList_hpp
#include <stdio.h>
#include "creatlist.h"
/*
剑指Offer(十五):反转链表
输入一个链表,反转链表后,输出新链表的表头。
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
*/
namespace codinginterviews {
#pragma mark - 循环实现
ListNode* ReverseList(ListNode* head) {
if (head == NULL || head->next == NULL) return head;
ListNode *pNode, *cNode, *nNode;
pNode = NULL;
cNode = head;
while(cNode != NULL){
nNode = cNode->next;
cNode->next = pNode;
pNode = cNode;
cNode = nNode;
}
return pNode;
}
#pragma mark - 递归实现
ListNode* ReverseListRecursion(ListNode* head){
if (head == NULL || head->next == NULL) return head;
ListNode *next = head->next;
head->next = NULL;
ListNode *pNode = ReverseListRecursion(next);
ListNode *newHead = pNode;
while(pNode->next != NULL){
pNode = pNode->next;
}
pNode->next = head;
return newHead;
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
ListNode* reverseBetween( ListNode* head, int m, int n){
int pos = 1;
ListNode *link1 = NULL, *link2=NULL, *ct = head;
for(; ct != NULL && pos < m - 1; pos++, ct = ct->next)
;
if (m != 1) {
link1 = ct;
ct = ct->next;pos++;
}
link2 = ct;
ListNode *pre, *next;
pre = NULL;
while(ct && pos != n+1){
next = ct->next;
ct->next = pre;
pre = ct;
ct = next;
pos++;
}
link2->next = ct;
if (m != 1){
link1->next = pre;
return head;
}
return pre;
}
ListNode* partition(ListNode* head, int x){
ListNode *lHead = NULL, *gHead = NULL, *node = head, *pToDeDelete = NULL, *pre = NULL, *next = NULL;
while (node) {
next = node->next;
if (node->val >= x){
//删除
pre->next = node->next;
if (!gHead) {
gHead = pToDeDelete = node;
}else{
pToDeDelete->next = node;
pToDeDelete = node;
}
}else{
pre = node;
if (!lHead) lHead = node;
}
node = next;
}
if (pToDeDelete)
pToDeDelete->next = NULL;
if (pre)
pre->next = gHead;
if (lHead)
return lHead;
return gHead;
}
void test_ReverseList(){
std::cout << "test_ReverseList staring.......... " << std::endl;
ListNode *root = creatLists({1,4,3,2,5,2})->next;
root = partition(root, 3);
root->print();
// root = ReverseList(root);
// root->print();
// root = ReverseListRecursion(root);
// root->print();
std::cout << "*******************" << std::endl;
}
}
#endif /* ReverseList_hpp */