[LintCode] Reorder List [链表综合题型]

news/2024/7/3 12:40:54

Problem

Given a singly linked list L: L0 → L1 → … → Ln-1 → Ln

reorder it to: L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …

Example

Given 1->2->3->4->null, reorder it to 1->4->2->3->null.

Challenge

Can you do this in-place without altering the nodes' values?

Note

链表题目的集合:双指针法找中点,分割,合并,翻转,排序。哈,都在这一题里面了。

主函数reorderList()
对于长度为0或1的链表,返回;找到中点mid;分割链表并翻转后半段为tail;与前半段head合并。

找中点findMid()
快慢指针,当fast == null || fast.next == null时,返回慢指针。

翻转reverse()
建立空链表结点pre,先存head.nexttemp,令head指向pre,令pre等于head,再令temphead。这样翻转就会令链表的首元素head移动到尾部,并让pre移动到所有已翻转结点的头部。当head移动到最后一个元素nullpre正好移动到整个链表的头部。返回pre,翻转完毕。

合并merge()
建立新链表结点dummy,复制到新链表结点cur。用index判断当前结点是奇数还是偶数。偶数则加入n1的结点,n1后移;否则加入n2的结点,n2后移。每加入一个结点后,cur后移。最后若n1n2有剩余结点,则加入cur.next。返回dummy.next

Solution

public class Solution {
    public void reorderList(ListNode head) {  
        if (head == null || head.next == null) return;
        ListNode mid = findMid(head);
        ListNode tail = reverse(mid.next);
        mid.next = null;
        merge(head, tail);
    }
    public ListNode findMid(ListNode head) {
        ListNode slow = head, fast = head.next;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public ListNode reverse(ListNode head) {
        ListNode pre = null;
        while (head != null) {
            ListNode temp = head.next;
            head.next = pre;
            pre = head;
            head = temp;
        }
        return pre;
    }
    public ListNode merge(ListNode n1, ListNode n2) {
        int index = 0;
        ListNode dummy = new ListNode(0);
        ListNode cur = dummy;
        while (n1 != null && n2 != null) {
            if (index % 2 == 0) {
                cur.next = n1;
                n1 = n1.next;
            } 
            else {
                cur.next = n2;
                n2 = n2.next;
            }
            index++;
            cur = cur.next;
        }
        if (n1 != null) cur.next = n1;
        else cur.next = n2;
        return dummy.next;
    }
}

http://www.niftyadmin.cn/n/1903688.html

相关文章

VirtualBox开机后黑屏

1、用管理员权限运行CMD 2、执行 netsh winsock reset 3、重启电脑

人工智能、深度学习、神经元网络常识

人工智能(Artificial Intelligence),英文缩写为AI。它是研究、开发用于模拟、延伸和扩展人的智能的理论、方法、技术及应用系统的一门新的技术科学。 人工智能是计算机科学的一个分支,它企图了解智能的实质,并生产出一…

linux下JLink使用脚本一个命令烧录程序

编写shell脚本program_bin,内容如下: #!/bin/bash#-------------------------------------------------- # program binfile, usage: ./program firmware.bin/hex # chip:Atmel # start addr:0x00400000 #--------------------------------------------…

python web部署

为什么80%的码农都做不了架构师?>>> nginx gunicorn flask supervisor的搭建 为什么要加nginx。 Nginx能更好地直接处理静态资源(通过一些http request header),而把动态资源转发给后端服务器 Nginx也可以缓存一些…

Android各种屏幕适配原理

dip(dp): device independent pixels(设备独立像素) dip,就是把屏幕的高分成480分,宽分成320分。比如你做一条160dip的横线,无论你在320还480的模拟器上,都是一半屏的长度。 dpi:dot per inch dpi&#xff…

Microsoft Dynamics CRM 2013 试用之系统篇 Windows Server 2012 R2安装

Microsoft Dynamics CRM 2013要求Windows Server 2008 R2以上,本人就装最新的Windows Server 2012 R2 测试。 系统安装很简单,直接下载原版iso载入虚拟机运行即可以。 以下为一般步骤: 选第二个,选第一个的话,就变成DO…

Nuttx实时操作系统 SAMV71-Xplained开发板适配

官网参考: https://nuttx.apache.org/docs/latest/quickstart/compiling.html 1、board配置 nhfnhf-VirtualBox:~/work/learn/nuttx/NUTTX/nuttx$ ./tools/configure.sh -L | grep samv71samv71-xult:nshsamv71-xult:netnshsamv71-xult:vncsamv71-xult:modulesamv…

集合框架(ListIterator的特有功能)

ListIterator接口的成员方法boolean hasPrevious()E previous()package cn.itcast_04;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.ListIterator;/** 列表迭代器:* ListIterator listIterator():Lis…