2.2 Implement an algorithm to find the kth to last element of a singly linked list.

Just using "runner" tech,let the first pointer to run for k step, then start the second pointer, at the same speed.

When the first one hit the end, the second one is just on the kth element.

2.5

(1) input: (7 -> 1 -> 6) + (5 -> 9 -> 2) , that is 617 + 295

 output: (2 -> 1 -> 9), that is 912.

solution: int add = new int[max(linklist1.length,linklist2.length) + 1];

7 -> 1 -> 6

         5 -> 9 -> 2

add:      0,    1,     1,    0
            2 -> 1 -> 9

(2) input: (6 -> 1 -> 7) + (2 -> 9 -> 5),that is 617 + 295

output: (9 -> 1 -> 2)

solution: just add by index, put the number larger than 10 in to array. then add again, until all the number in array is 0.

6 -> 1 -> 7

2 -> 9 -> 5

add: 8 ->10->12   >>=>>  8 -> 0 -> 2

1,    1,     0

9 -> 1 -> 2

2.6

题目:①判断一个单向链表是否有环,如果有环则找到环的入口节点。

②判断两个单向链表是否相交,如果相交则找到交点节点。

算法思想:①用两个指针p1,p2同时指向链表的头部,p1一次移动一步,p2一次移动两步,如果最终p1和p2重合则说明链表有环,如果p2走到空指针(链表的结尾)则说明链表无环。如果最终p1和p2重合,使p2重新指向链表的头结点,然后p1和p2同时一次移动一步,当p1和p2再次重合时该节点指针就是环的入口节点指针。

给定一个有环的链表,写一个算法,找出环的起点。

例如:输入:A->B->C->D->E->C[与前面的C是同一个节点]

输出:C

判断一个链表是否存在环有一个简单的方法:就是使用一个快指针、和一个慢指针,快指针每次走两步,慢指针每次走一步,则如果有环,它们最后必然会相遇的。

本题的难点在于要找出环的起点。其实也不难,与判断是否有环类似,用两个步长分别为1和2的指针遍历链表,直到两者相遇,此时慢指针走过的长度就是环的长度。

另外相遇后把其中指针重新设定为起始点,让两个指针以步长1再走一遍链表,相遇点就是环的起始点。

证明也很简单,证明如下:我们注意到第一次相遇时:慢指针走过的路程S1 = 非环部分长度 + 弧A长,

                            快指针走过的路程S2 = 非环部分长度 + n * 环长 + 弧A长,

                        S1 * 2 = S2,

可得 非环部分长度 = n * 环长 - 弧A长。

让指针A回到起始点后,走过一个非环部分长度,指针B走过了相等的长度,也就是n * 环长 - 弧A长,正好回到环的开头。

②有了第一问的算法基础,应该不难理解第二问。首先将其中一个链表list1首尾相接,变成一个有环链表,如果另一个链表list2和list1相交的话,list2也将成为一个有环链表,并且环的入口节点就是两个链表的交叉节点。如果两个链表不相交,则list2依然是一个无环链表。

2.7 palindrome 首先考虑使用stack来存储,这样极其简洁。

Chp2: Linked List的更多相关文章

  1. [LeetCode] Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  2. [LeetCode] Plus One Linked List 链表加一运算

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  3. [LeetCode] Odd Even Linked List 奇偶链表

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  4. [LeetCode] Delete Node in a Linked List 删除链表的节点

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  5. [LeetCode] Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...

  6. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  7. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  8. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

随机推荐

  1. svn 分支整个项目合并主干

    1.首先主干要更新最新版本. 2.找到主干(trunk)点击右键--合并--合并类型选择(合并一个版本范围)点击下一步--合并源选择整个分支项目--将要合并的修改版本范围(选择指定(a)范围)点击下一 ...

  2. 浏览器userAgent大全

    iPhone ●iOSMozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Versi ...

  3. 实例介绍Cocos2d-x物理引擎:HelloPhysicsWorld

    我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用物理引擎的开发过程,熟悉这些API的使用.这个实例的运行后的场景,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触摸点生成一个新的 ...

  4. Could not load the "defaultimg" image referenced from a nib in the bundle with identifier "com.abc"

    出现这个错误提示,是因为在xib中使用了.jpg格式的图片,在图片名称后面加上.jpg即可;

  5. stl的实现原理简单讲解,通熟易懂

    总结 需要经常随机访问请用vector 2.list list就是双向链表,元素也是在堆中存放,每个元素都是放在一块内存中,它的内存空间可以是不连续的,通过指针来进行数据的访问,这个特点使得它的随机存 ...

  6. shadow服务端、客户端配置流程

    服务端 系统环境 CentOS 7 64位,由于系统自带python,shadowsocks服务端我们选择python版,过程如下 yum install python-setuptools & ...

  7. Mac下使用Web服务器性能/压力测试工具webbench、ab、siege

    Web开发,少不了的就是压力测试,它是评估一个产品是否合格上线的基本标准,下面我们来一一剖析他们的使用方式. 测试前,前面先把系统的端口限制数改大,看看Mac下面的默认限制 ulimit -a ope ...

  8. Spark Streaming揭秘 Day5 初步贯通源码

    Spark Streaming揭秘 Day5 初步贯通源码 引子 今天,让我们从Spark Streaming最重要的三个环节出发,让我们通过走读,逐步贯通源码,还记得Day1提到的三个谜团么,让我们 ...

  9. 宝马-中国官方网站服务站点信息爬去记录(解析json中数据)

    具体步骤: 1.进入宝马官网,查找经销商查询界面 http://www.bmw.com.cn/cn/zh/general/dealer_locator/content/dealer_locator.h ...

  10. 删除_desktop.ini病毒文件

    del h:\_desktop.ini /f/s/q/a/f 强制删除只读文件/s 从当前目录及其所有子目录栓出指定文件.显示正在删除的文件名/q 制定清音状态.不提示确认删除/a 按照属性来删除