-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListNode.h
More file actions
63 lines (55 loc) · 1.36 KB
/
ListNode.h
File metadata and controls
63 lines (55 loc) · 1.36 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
#ifndef __LISTNODE_H__
#define __LISTNODE_H__
#include "Header.h"
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode * genRandList(int len, int maxVal){
srand((int)time(0));
ListNode *head = new ListNode(random(maxVal));
ListNode *q = head;
for (int i = 1; i <= len; ++i)
{
q->next = new ListNode(random(maxVal));
q = q->next;
}
return head;
}
ListNode * genOrderList(int len){
ListNode *head = new ListNode(1);
ListNode *q = head;
for (int i = 2; i <= len; ++i)
{
q->next = new ListNode(i);
q = q->next;
}
return head;
}
ListNode * genListFromStr(string str){
ListNode *head = new ListNode(0), *tmp = head;
int i = 0, j;
while (i < str.length()) {
while (i < str.length() && (str[i] < '0' || str[i] > '9')) i++;
j = 0;
for (; i+j < str.length() && str[i+j] <= '9' && str[i+j] >= '0'; j++);
int t = atoi(str.substr(i, j).c_str());
tmp->next = new ListNode(t);
tmp = tmp->next;
i += j;
}
return head->next;
}
string toString(ListNode *head) {
string res = "[";
ListNode *t = head;
while(t->next){
res += to_string(t->val) + ", ";
t = t->next;
}
res += to_string(t->val) + "]";
return res;
}
#endif