Careercup | Chapter 2
链表的题里面,快慢指针、双指针用得很多。
2.1 Write code to remove duplicates from an unsorted linked list.
FOLLOW UP
How would you solve this problem if a temporary buffer is not allowed?
2.2 Implement an algorithm to find the kth to last element of a singly linked list.
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
2.4 Write code to partition a linked list around a value x, such that all nodes less than x come before alt nodes greater than or equal to x.
Leetcode上有,点此。
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1 's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
Leetcode上有,点此。
FOLLOW UP
Suppose the digits are stored in forward order. Repeat the above problem.
reverse之后求后然后再reverse结果,careercup上的做法更inefficient。
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.
Leetcode上有,点此。
2.7 Implement a function to check if a linked list is a palindrome,
naive的方法就是把list reverse一下,然后和原串比较。
更好的方法是用stack,比较前半部分还是很巧妙的。stack用来reverse也是比较直观的。注意奇偶长度的list。
递归的方法理解起来更难些。传递指针的指针,使得递归调用后,指针move到对应的镜像位置上了。这一点和Leetcode上Convert Sorted List to Binary Search Tree类似。
struct ListNode {
int val;
ListNode* next;
ListNode(int v) : val(v), next(NULL) {}
};
class XList {
public:
XList(int n) {
srand(time(NULL));
head = NULL;
for (int i = ; i < n; ++i) {
ListNode* next = new ListNode(rand() % );
next->next = head;
head = next;
}
len = n;
}
XList(XList ©) {
//cout << "copy construct" << endl;
len = copy.size();
if (len == ) return;
head = new ListNode(copy.head->val);
ListNode *p = copy.head->next, * tail = head;
while (p != NULL) {
tail->next = new ListNode(p->val);
tail = tail->next;
p = p->next;
}
}
~XList() {
ListNode *tmp = NULL;
while (head != NULL) {
tmp = head->next;
delete head;
head = tmp;
}
}
// 2.1(1)
void removeDups() {
if (head == NULL) return;
map<int, bool> existed;
ListNode* p = head, *pre = NULL;
while (p != NULL) {
if (existed[p->val]) {
pre->next = p->next;
len--;
delete p;
p = pre->next;
} else {
pre = p;
existed[p->val] = true;
p = p->next;
}
}
}
//2.1(2)
void removeDups2() {
ListNode *p = head;
while (p != NULL) {
ListNode *next = p;
while (next->next) {
if (next->next->val == p->val) {
ListNode *tmp = next->next;
len--;
delete next->next;
next->next = tmp->next;
} else {
next = next->next; // only move to next in the 'else' block
}
}
p = p->next;
}
}
// 2.2(1)
ListNode* findKthToLast(int k) {
if (head == NULL) return NULL;
if (k <= ) return NULL; // more efficient
ListNode *fast = head, *slow = head;
int i = ;
for (; i < k && fast; ++i) {
fast = fast->next;
}
if (i < k) return NULL;
while (fast) {
slow = slow->next;
fast = fast->next;
}
return slow;
}
//2.2(2)
ListNode* findKthToLast2(int k) {
return recursiveFindKthToLast(head, k);
}
//2.2(2)
ListNode* recursiveFindKthToLast(ListNode *h, int &k) {
if (h == NULL) {
return NULL;
}
// should go to the end
ListNode *ret = recursiveFindKthToLast(h->next, k);
k--;
if (k == ) return h;
return ret;
}
// 2.3
bool deleteNode(ListNode* node) {
if (node == NULL || node->next == NULL) return false; // in the middle, head is also ok, because we don't delete node itself
ListNode *next = node->next;
node->val = next->val;
node->next = next->next;
len--;
delete next;
return true;
}
// 2.4
void partition(int x) {
if (head == NULL) return;
ListNode less(), greater();
ListNode* p = head, *p1 = &less, *p2 = &greater;
while (p) {
if (p->val < x) {
p1->next = p;
p1 = p1->next;
} else {
p2->next = p;
p2 = p2->next;
}
p = p->next;
}
p1->next = greater.next;
head = less.next;
}
// 2.7(1)
bool isPalindrome() {
if (head == NULL) return true;
stack<ListNode*> st;
ListNode *fast = head, *slow = head;
while (fast && fast->next) {
st.push(slow);
slow = slow->next;
fast = fast->next->next;
}
if (fast) slow = slow->next; // fast->next = null, odd number, skip the middle one
while (slow) {
if (slow->val != st.top()->val) return false;
slow = slow->next;
st.pop();
}
return true;
}
// 2.7(2)
bool isPalindrome2() {
ListNode* h = head;
return recursiveIsPalindrome(h, len);
}
bool recursiveIsPalindrome(ListNode* &h, int l) { // note that h is passed by reference
if (l <= ) return true;
if (l == ) {
h = h->next; // move, when odd
return true;
}
if (h == NULL) return true;
int v1 = h->val;
h = h->next;
if (!recursiveIsPalindrome(h, l - )) return false;
int v2 = h->val;
h = h->next;
cout << v1 << " vs. " << v2 << endl;
return v1 == v2;
}
void print() const {
ListNode *p = head;
while (p != NULL) {
cout << p->val << "->";
p = p->next;
}
cout << "NULL(len: " << len << ")" << endl;
}
int size() const {
return len;
}
void insert(int v) {
len++;
ListNode *node = new ListNode(v);
node->next = head;
head = node;
}
private:
ListNode *head;
int len;
};
Careercup | Chapter 2的更多相关文章
- Careercup | Chapter 1
1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot us ...
- Careercup | Chapter 3
3.1 Describe how you could use a single array to implement three stacks. Flexible Divisions的方案,当某个栈满 ...
- Careercup | Chapter 8
8.2 Imagine you have a call center with three levels of employees: respondent, manager, and director ...
- Careercup | Chapter 7
7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...
- CareerCup Chapter 9 Sorting and Searching
9.1 You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. ...
- CareerCup chapter 1 Arrays and Strings
1.Implement an algorithm to determine if a string has all unique characters What if you can not use ...
- CareerCup Chapter 4 Trees and Graphs
struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int val):val(val),left(NULL),rig ...
- Careercup | Chapter 6
6.2 There is an 8x8 chess board in which two diagonally opposite corners have been cut off. You are ...
- Careercup | Chapter 5
5.1 You are given two 32-bit numbers, N andM, and two bit positions, i and j. Write a method to inse ...
随机推荐
- 微软与百度合作:win10搜索引擎默认百度
全球最大的中文搜索引擎百度公司与微软公司共同宣布双方展开战略合作.百度并将成为中国市场上Windows 10 Microsoft Edge浏览器的默认主页和搜索引擎.也就是说,将来人们在win10的M ...
- BFS、模拟:UVa1589/POJ4001/hdu4121-Xiangqi
Xiangqi Xiangqi is one of the most popular two-player board games in China. The game represents a ba ...
- 用Comparator排序和分组
Test实体 import java.util.Objects; /** * @author gallen * @description * @date 2018/11/16 * @time 18:5 ...
- Hadoop4.2HDFS测试报告之六
测试结论 第一组数据作表格作图: 第二组数据作表格作图: 根据以上图分析得出以下结论: 1. 本地存储的读写速率基本保持23M左右,说明本地存储比较稳定. 2. HDFS存储两个数据节点的读写速率性能 ...
- Python虚拟机函数机制之参数类别(三)
参数类别 我们在Python虚拟机函数机制之无参调用(一)和Python虚拟机函数机制之名字空间(二)这两个章节中,分别PyFunctionObject对象和函数执行时的名字空间.本章,我们来剖析一下 ...
- IE6 单文件绿色版
IE6单文件绿色版,可以直接运行,无需安装,完美兼容Win10(自带2016年1月更新). https://www.lanzous.com/i3w7dej
- logging——日志
导读 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,log ...
- python学习--同目录下调用 (*.py)及不同目录下调(*.py)
注:__init__.py 内容为空 1. 同目录下调用 (Contract_Statelog.py) 如图: temp1.py 调用 Contract_Statelog.py中的方法 2. 不同目 ...
- matlab 初级画图
matlab 初级画图 1.plot() plot(x,y) plots each vector pairs (x,y) 画图函数画出每个点 每组变量 plot (y) plots eac ...
- Welcome-to-Swift-13继承(Inheritance)
一个类可以继承(inherit)另一个类的方法(methods),属性(property)和其它特性.当一个类继承其它类时,继承类叫子类(subclass),被继承类叫超类(或父类,superclas ...