GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — [LeetCode] 143. Reorder List · Issue #143 · grandyang/leetcode Open Description
Given a singly linked list L : L 0→ L 1→…→ L n -1→ L n, You may not modify the values in the list's nodes, only nodes itself may be changed. Example 1: Example 2:
这道链表重排序问题可以拆分为以下三个小问题: 1. 使用快慢指针来找到链表的中点,并将链表从中点处断开,形成两个独立的链表。 2. 将第二个链翻转。 3. 将第二个链表的元素间隔地插入第一个链表中。
解法一:
我们尝试着看能否写法上简洁一些,上面的第二步是将后半段链表翻转,那么我们其实可以借助栈的后进先出的特性来做,如果我们按顺序将所有的结点压入栈,那么出栈的时候就可以倒序了,实际上就相当于翻转了链表。由于只需将后半段链表翻转,那么我们就要控制出栈结点的个数,还好栈可以直接得到结点的个数,我们减1除以2,就是要出栈结点的个数了。然后我们要做的就是将每次出栈的结点隔一个插入到正确的位置,从而满足题目中要求的顺序,链表插入结点的操作就比较常见了,这里就不多解释了,最后记得断开栈顶元素后面的结点,比如对于 1->2->3->4,栈顶只需出一个结点4,然后加入原链表之后为 1->4->2->3->(4),因为在原链表中结点3之后是连着结点4的,虽然我们将结点4取出插入到结点1和2之间,但是结点3后面的指针还是连着结点4的,所以我们要断开这个连接,这样才不会出现环,由于此时结点3在栈顶,所以我们直接断开栈顶结点即可,参见代码如下:
解法二:
参考资料: https://leetcode.com/problems/reorder-list/ https://leetcode.com/problems/reorder-list/discuss/45175/Java-solution-with-stack https://leetcode.com/problems/reorder-list/discuss/44992/Java-solution-with-3-steps
ActivityMetadataMetadataAssigneesLabelsNo labels TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |