链表的题里面,快慢指针、双指针用得很多。

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 &copy) {
//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的更多相关文章

  1. Careercup | Chapter 1

    1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot us ...

  2. Careercup | Chapter 3

    3.1 Describe how you could use a single array to implement three stacks. Flexible Divisions的方案,当某个栈满 ...

  3. Careercup | Chapter 8

    8.2 Imagine you have a call center with three levels of employees: respondent, manager, and director ...

  4. Careercup | Chapter 7

    7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...

  5. 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. ...

  6. 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 ...

  7. CareerCup Chapter 4 Trees and Graphs

    struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int val):val(val),left(NULL),rig ...

  8. Careercup | Chapter 6

    6.2 There is an 8x8 chess board in which two diagonally opposite corners have been cut off. You are ...

  9. 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 ...

随机推荐

  1. 【 android】When an app is installed on the external storage

    When an app is installed on the external storage: The .apk file is saved to the external storage, bu ...

  2. Python之简单Socket编程

    Socket编程这块儿还是比较重要的,记录一下:实现服务器端和客户端通信(客户端发送系统指令,如ipconfig等,服务器端执行该指令,然后将指令返回结果给客户端再传过去,设置一次最多直接收1024字 ...

  3. 使用fio测试磁盘I/O性能

    简介: fio是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, nu ...

  4. CDH4 journalnode方式手工安装手册之一

    一.                                环境部署概况   cdh-master 172.168.10.251 cdh-node1 172.168.10.251 cdh-no ...

  5. LA 5007 Detector Placement 模拟

    题意: 给出一束光线(射线),和一块三角形的棱镜 以及 棱镜的折射率,问光线能否射到X轴上,射到X轴上的坐标是多少. 分析: 其实直接模拟就好了,注意到题目中说不会发生全反射,所以如果射到棱镜中的话就 ...

  6. luogu2153 [SDOI2009]晨跑

    要想限制流量,总要想着拆点. #include <iostream> #include <cstring> #include <cstdio> #include & ...

  7. Spring core resourc层结构体系及JDK与Spring对classpath中资源的获取方式及结果对比

    1. Spring core resourc层结构体系 1.1. Resource相关结构体系 1.2. ResourceLoader相关体系 2. JDK与Spring对classpath中资源的获 ...

  8. day03_08 变量的重新赋值02

    python自动回收垃圾内存,不用了自动会回收,但是C不会 以下del代码为手动强拆,就是从内存中删除变量名

  9. 正则表达式 去除所有非ASCII字符

    需求: 去除字符串中包含的所有外国字符 只能使用正则如下,找到包含非ASCII的记录 db=# select * from test where info ~ '[^(\x00-\x7f)]'; id ...

  10. 九度oj 题目1368:二叉树中和为某一值的路径

    题目描述: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 输入: 每个测试案例包括n+1行: 第一行为2 ...