重排链表
来源:互联网 发布:ubuntu查找文件命令 编辑:程序博客网 时间:2024/06/08 08:41
题目:给定一个单链表L: L0→L1→…→Ln-1→Ln, 重新排列后为:L0→Ln→L1→Ln-1→L2→Ln-2→…, 必须在不改变节点值的情况下进行原地操作
例子:给出链表1->2->3->4->null,重新排列后为1->4->2->3->null。
步骤:
- 把整个链表划分成2个等长的子链表,如果原链表长度为奇数,那么第一个子链表的长度多1。
- 翻转第二个子链表。
- 将两个子链表合并。
public class Solution { private ListNode reverse(ListNode head) { ListNode newHead = null; while (head != null) { ListNode temp = head.next; head.next = newHead; newHead = head; head = temp; } return newHead; } private void merge(ListNode head1, ListNode head2) { int index = 0; ListNode dummy = new ListNode(0); while (head1 != null && head2 != null) { if (index % 2 == 0) { dummy.next = head1; head1 = head1.next; } else { dummy.next = head2; head2 = head2.next; } dummy = dummy.next; index ++; } if (head1 != null) { dummy.next = head1; } else { dummy.next = head2; } } private ListNode findMiddle(ListNode head) { ListNode slow = head, fast = head.next; while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; } return slow; } public void reorderList(ListNode head) { if (head == null || head.next == null) { return; } ListNode mid = findMiddle(head); ListNode tail = reverse(mid.next); mid.next = null; merge(head, tail); }}
0 0
- 重排链表
- 重排链表
- 重排链表
- 重排链表
- LintCode:重排链表
- LintCode : 重排链表
- 重排链表
- 重排链表
- 重排链表-LintCode
- Reorder List 重排链表
- 算法:链表的重排
- Reorder List 链表重排
- L2-022. 重排链表
- lintcode(99)重排链表
- L2-022. 重排链表
- L2-022. 重排链表
- Java实现-重排链表
- L2-022. 重排链表
- Android设置默认Launcher
- memcached CAS
- 本地推送通知和远程推送通知
- Spring注解@Component、@Repository、@Service、@Controller区别
- Linux下的Socket编程
- 重排链表
- Linux多线程编程
- 安全狗之绕过注入
- Android反调试之 AntiEmulator 检测安卓模拟器
- 【Linux网络编程】广播地址介绍
- iOS触摸事件总结
- 如何在linux下检测内存泄漏
- poj 3159 Candies(优先队列 dijkstra+堆)
- GTK+浅谈之五等待旋转动画