Reorder List Java

来源:互联网 发布:万德数据库打不开 编辑:程序博客网 时间:2024/05/22 01:45
</pre><div class="row" style="box-sizing: border-box; margin-right: -15px; margin-left: -15px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14.399999618530273px; line-height: 16px;"><div class="col-md-12" style="box-sizing: border-box; position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px;"><div class="row" style="box-sizing: border-box; margin-right: -15px; margin-left: -15px;"><div class="col-md-12" style="box-sizing: border-box; position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px;"><div class="question-content" style="box-sizing: border-box; margin-left: 20px; margin-top: 20px; margin-bottom: 5px; line-height: 30px; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(221, 221, 221); padding-left: 20px; padding-bottom: 2px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">Given a singly linked list <span style="box-sizing: border-box;">L</span>: <span style="box-sizing: border-box;">L</span><span style="box-sizing: border-box; position: relative; font-size: 10.399999618530273px; line-height: 0; vertical-align: baseline; bottom: -0.25em;">0</span>→<span style="box-sizing: border-box;">L</span><span style="box-sizing: border-box; position: relative; font-size: 10.399999618530273px; line-height: 0; vertical-align: baseline; bottom: -0.25em;">1</span>→…→<span style="box-sizing: border-box;">L</span><span style="box-sizing: border-box; position: relative; font-size: 10.399999618530273px; line-height: 0; vertical-align: baseline; bottom: -0.25em;"><span style="box-sizing: border-box;">n</span>-1</span>→<span style="box-sizing: border-box;">L</span><span style="box-sizing: border-box; position: relative; font-size: 10.399999618530273px; line-height: 0; vertical-align: baseline; bottom: -0.25em;">n</span>,<br style="box-sizing: border-box;" />reorder it to: <span style="box-sizing: border-box;">L</span><span style="box-sizing: border-box; position: relative; font-size: 10.399999618530273px; line-height: 0; vertical-align: baseline; bottom: -0.25em;">0</span>→<span style="box-sizing: border-box;">L</span><span style="box-sizing: border-box; position: relative; font-size: 10.399999618530273px; line-height: 0; vertical-align: baseline; bottom: -0.25em;"><span style="box-sizing: border-box;">n</span></span>→<span style="box-sizing: border-box;">L</span><span style="box-sizing: border-box; position: relative; font-size: 10.399999618530273px; line-height: 0; vertical-align: baseline; bottom: -0.25em;">1</span>→<span style="box-sizing: border-box;">L</span><span style="box-sizing: border-box; position: relative; font-size: 10.399999618530273px; line-height: 0; vertical-align: baseline; bottom: -0.25em;"><span style="box-sizing: border-box;">n</span>-1</span>→<span style="box-sizing: border-box;">L</span><span style="box-sizing: border-box; position: relative; font-size: 10.399999618530273px; line-height: 0; vertical-align: baseline; bottom: -0.25em;">2</span>→<span style="box-sizing: border-box;">L</span><span style="box-sizing: border-box; position: relative; font-size: 10.399999618530273px; line-height: 0; vertical-align: baseline; bottom: -0.25em;"><span style="box-sizing: border-box;">n</span>-2</span>→…</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">You must do this in-place without altering the nodes' values.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">For example,<br style="box-sizing: border-box;" />Given <code style="box-sizing: border-box; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12.800000190734863px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(249, 242, 244);">{1,2,3,4}</code>, reorder it to <code style="box-sizing: border-box; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12.800000190734863px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(249, 242, 244);">{1,4,2,3}</code>.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"></p></div></div></div></div></div><div id="interviewed_div" style="box-sizing: border-box; padding-left: 20px; padding-top: 12px; padding-bottom: 2px; font-size: 12px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><form id="interviewed_form" method="post" action="https://oj.leetcode.com/interviewed/" class="ng-pristine ng-valid" style="box-sizing: border-box;"><span style="box-sizing: border-box;">Have you been asked this question in an interview?</span> </form></div><span style="font-size:14px;"> * key to solve:<span style="white-space:pre"></span> * 1.Use two points: slow and fast node to locate middle of linked list 1.1<span style="white-space:pre"></span> * then separate it into two linked list<span style="white-space:pre"></span> * <span style="white-space:pre"></span> * 2.reverse second half of linked list <span style="white-space:pre"></span> * before reverse<span style="white-space:pre"></span> *  1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null <span style="white-space:pre"></span> *  <span style="white-space:pre"></span>    |<span style="white-space:pre"></span>   |<span style="white-space:pre"></span> *  <span style="white-space:pre"></span> mid second <span style="white-space:pre"></span> *  after reverse<span style="white-space:pre"></span> *  1 -> 2 -> 3 -> 6 -> 5 -> 4 -> null<span style="white-space:pre"></span> * <span style="white-space:pre"></span>  |<span style="white-space:pre"></span>   |<span style="white-space:pre"></span> *  <span style="white-space:pre"></span> mid second </span><p><span style="font-size:14px;"><span style="white-space:pre"></span> *  3. Combine these two linked list together into one</span></p><p><span style="font-size:14px;"></span><pre name="code" class="java">public class Solution {    public void reorderList(ListNode head) {        if (head != null && head.next != null) {ListNode slow = head;ListNode fast = head;while (fast != null && fast.next != null && fast.next.next != null) {slow = slow.next;fast = fast.next.next;}// separate it into two linked listListNode secondHalf = slow.next;slow.next = null;// reverse second half linked listsecondHalf = reverse(secondHalf);// combine two linked list togetherListNode list1 = head;ListNode list2 = secondHalf;while (list1 != null && list2 != null) {// save next fist and second half nodeListNode t1 = list1.next;ListNode t2 = list2.next;// connect into onelist1.next = list2;list2.next = t1;list1 = t1;list2 = t2;}}    }    // reverse linked list functionpublic ListNode reverse(ListNode head) {ListNode dummy = new ListNode(0);dummy.next = head;ListNode prev = dummy;ListNode last = head;ListNode cur = last.next;while (cur != null) {last.next = cur.next;cur.next = prev.next;prev.next = cur;cur = last.next;}return dummy.next;}}


0 0