长颈鹿Giraffe

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

2013年7月24日

摘要: Write code to partition a linked list around a value x, such that all nodes less than xcome before alt nodes greater than or equal to.分割一个链表,将值小于x的节点全部放在其他节点的前面。解答:我们可以创建两个不同的链表,其中一个用来连接比x小的节点,另外一个则连接大于等于x的节点,然后合并两个链表即可。public class Main { public static void appendToTail(List head, int d) { ... 阅读全文
posted @ 2013-07-24 12:12 长颈鹿Giraffe 阅读(490) 评论(0) 推荐(0) 编辑

摘要: Implement an algorithm to delete a node in the middle of a singly linked list,given only access to that node.EXAMPLEInput: the node c from the linked list a->b->c->d->eResult: nothing is returned, but the new linked list looks like a- >b- >d->e删除链表中的一个节点,但并没有给出链表的头结点,而是只能访问要删除的这 阅读全文
posted @ 2013-07-24 11:34 长颈鹿Giraffe 阅读(329) 评论(0) 推荐(0) 编辑

摘要: Implement an algorithm to find the kth to last element of a singly linked list.实现一个算法寻找链表中倒数第K个数..解答:关于链表的问题,书中也提到了关键的两个技巧,一个是递归;另一个是The"Runner"Technique,也就是使用两个指针同时遍历链表的方式。这道题可以分别用这两种方法来解决。import java.util.HashMap;public class List { int data; List next; public List(int d) { this.da... 阅读全文
posted @ 2013-07-24 11:07 长颈鹿Giraffe 阅读(307) 评论(0) 推荐(0) 编辑