把一个链表居中切分红两个,系列目录见 前言和目录 。javascript
实现函数 frontBackSplit()
把链表居中切分红两个子链表 -- 一个前半部分,另外一个后半部分。若是节点数为奇数,则多余的节点应该归类到前半部分中。例子以下,注意 front
和 back
是做为空链表被函数修改的,因此这个函数不须要返回值。java
var source = 1 -> 3 -> 7 -> 8 -> 11 -> 12 -> 14 -> null var front = new Node() var back = new Node() frontBackSplit(source, front, back) front === 1 -> 3 -> 7 -> 8 -> null back === 11 -> 12 -> 14 -> null
若是函数的任何一个参数为 null
或者原链表长度小于 2 ,应该抛出异常。node
提示:一个简单的作法是计算链表的长度,而后除以 2 得出前半部分的长度,最后分割链表。另外一个方法是利用双指针。一个 “慢” 指针每次遍历一个节点,同时一个 ”快“ 指针每次遍历两个节点。当快指针遍历到末尾时,慢指针正好遍历到链表的中段。git
这个 kata 主要考验的是指针操做,因此解法用不上递归。github
代码以下:segmentfault
function frontBackSplit(source, front, back) { if (!front || !back || !source || !source.next) throw new Error('invalid arguments') const array = [] for (let node = source; node; node = node.next) array.push(node.data) const splitIdx = Math.round(array.length / 2) const frontData = array.slice(0, splitIdx) const backData = array.slice(splitIdx) appendData(front, frontData) appendData(back, backData) } function appendData(list, array) { let node = list for (const data of array) { if (node.data !== null) { node.next = new Node(data) node = node.next } else { node.data = data } } }
解法思路是把链表变成数组,这样方便计算长度,也方便用 slice
方法分割数组。最后用 appendData
把数组转回链表。由于涉及到屡次遍历,这并非一个高效的方案,并且还须要一个数组处理临时数据。数组
代码以下:app
function frontBackSplitV2(source, front, back) { if (!front || !back || !source || !source.next) throw new Error('invalid arguments') let len = 0 for (let node = source; node; node = node.next) len++ const backIdx = Math.round(len / 2) for (let node = source, idx = 0; node; node = node.next, idx++) { append(idx < backIdx ? front : back, node.data) } } // Note that it uses the "tail" property to track the tail of the list. function append(list, data) { if (list.data === null) { list.data = data list.tail = list } else { list.tail.next = new Node(data) list.tail = list.tail.next } }
这个解法经过遍历链表来获取总长度并算出中间节点的索引,算出长度后再遍历一次链表,而后用 append
方法选择性地把节点数据加入 front
或 back
两个链表中去。这个解法不依赖中间数据(数组)。函数
append
方法有个值得注意的地方。通常状况下把数据插入链表的末尾的空间复杂度是 O(n) ,为了不这种状况 append
方法为链表加了一个 tail
属性并让它指向尾节点,让空间复杂度变成 O(1) 。测试
代码以下:
function frontBackSplitV3(source, front, back) { if (!front || !back || !source || !source.next) throw new Error('invalid arguments') let slow = source let fast = source while (fast) { // use append to copy nodes to "front" list because we don't want to mutate the source list. append(front, slow.data) slow = slow.next fast = fast.next && fast.next.next } // "back" list just need to copy one node and point to the rest. back.data = slow.data back.next = slow.next }
思路在开篇已经有解释,当快指针遍历到链表末尾,慢指针正好走到链表中部。但如何修改 front
和 back
两个链表仍是有点技巧的。
对于 front
链表,慢指针每次遍历的数据就是它须要的,因此每次遍历时把慢指针的数据 append
到 front
链表中就行(第 9 行)。
对于 back
链表,它所需的数据就是慢指针停下的位置到末尾。咱们不用复制整个链表数据到 back
,只用复制第一个节点的 data
和 next
便可。这种 复制头结点,共用剩余节点 的技巧常常出如今一些 Immutable Data 的操做中,以省去没必要要的复制。这个技巧其实也能够用到上一个解法里。