-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_for_list.cpp
More file actions
90 lines (77 loc) · 1.71 KB
/
test_for_list.cpp
File metadata and controls
90 lines (77 loc) · 1.71 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
// twst.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
struct ListNode
{
ListNode* next;
int var;
ListNode() : next(nullptr), var(0) {}
ListNode(int _var) : next(nullptr), var(_var) {}
};
//创建了一个没有头结点的单向链表
ListNode* MakeList(vector<int>& vec)
{
ListNode* head = new ListNode();
ListNode* cur = head;
for (auto& i : vec)
{
ListNode* tmp = new ListNode(i);
cur->next = tmp;
cur = tmp;
}
return head->next;
//return head //返回带头结点的链表
}
void PrintfList(ListNode* head)
{
while (head != nullptr)
{
cout << head->var << '\t';
head = head->next;
}
cout << endl;
}
ListNode* RevertList(ListNode* head)
{
if (head == nullptr && head->next == nullptr) return head;
ListNode* newList = nullptr;
while (head != nullptr)
{
ListNode* tmp = head;
head = head->next;
tmp->next = newList;
newList = tmp;
}
return newList;
}
ListNode* GetLastKthNode(ListNode* head, unsigned int k)
{
if (head == nullptr) return head;
ListNode* last = head;
for (unsigned int i=0;i<k;i++)
{
last = last->next;
if (last == nullptr)
{
cout << "unexpected k value!" << endl;
return nullptr;
}
}
while (last != nullptr)
{
head = head->next;
last = last->next;
}
return head;
}
int main()
{
vector<int> vec = { 1,2,3,4,5,6 };
ListNode* head = MakeList(vec);
PrintfList(head);
//PrintfList(RevertList(head);
cout << GetLastKthNode(head, 1)->var << endl;
}