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

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. Linux学习-systemctl 针对 timer 的配置文件

    如何使用 systemd 内建的 time 来处理各种任务呢? systemd.timer 的优势 在 archlinux 的官网 wiki 上面有提到,为啥要使用 systemd.timer 呢? ...

  2. python + selenium - selenium简介

    1. 产品简介 selenium 是 基于 web网页的UI自动化测试框架. 1)支持多浏览器操作:ie.chrome.firefox.edge.safaria等 2)跨平台:windows.linu ...

  3. python_字符串,元组,格式化输出

    一.字符串 1.字符串是有成对的单引号或者双引号括起来的.例如:name="张三",sex="女" 2.字符串的索引是从0开始的 3.字符串的切片 a.单个字符 ...

  4. 【水】ZYH记

    我从十二岁起,便在sdoj的蒟蒻餐厅里当伙计,root说,样子太傻,怕侍候不了专职切题,就在外面做点事罢.外面的调试管理,虽然容易说话,但唠唠叨叨缠夹不清的也很不少.他们往往要亲眼看着一个字一个字编译 ...

  5. niubi-job:一个分布式的任务调度框架设计原理以及实现

    niubi-job的框架设计是非常简单实用的一套设计,去掉了很多其它调度框架中,锦上添花但并非必须的组件,例如MQ消息通讯组件(kafka等).它的框架设计核心思想是,让每一个jar包可以相对之间独立 ...

  6. Vmware占用宿主机硬盘越来越大

    Vmware占用宿主机硬盘越来越大 root /usr/bin/vmware-toolbox-cmd disk shrink /

  7. php rabbitmq操作类及生产者和消费者实例代码 转

    注意事项: 1.accept.php消费者代码需要在命令行执行 2.'username'=>'asdf','password'=>'123456' 改成自己的帐号和密码 RabbitMQC ...

  8. Java接口抽象类

    抽象类中的方法可以实现,接口中的方法只能声明,不能实现.抽象类的成员变量可以为各种类型,接口的变量只能为public static final.抽象类可以有静态方法和静态代码块,接口不能有.一个类只能 ...

  9. 建立RSA协商加密的安全信道

    在基于TCP长连接的CS链路中,如何保证数据流的安全性是开发者最关注的问题之一.本文深入浅出的给大家介绍一下在TCP连接中,使用RSA协商加密的方式,建立一个安全加密的通信链路,保证数据传输的安全性. ...

  10. Educational Codeforces Round 11——A. Co-prime Array(map+vector)

    A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...