Leetcode 143.重排链表

重排链表

给定一个单链表 LL0L1→…→Ln-1Ln ,
将其重新排列后变为: L0LnL1Ln-1L2Ln-2→…

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

示例 1:

给定链表 1->2->3->4, 重新排列为 1->4->2->3.

示例 2:

给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.

 1 public class Solution{
 2     public void reorderList(ListNode head){
 3         if(head==null) return;
 4         ListNode slow=head,fast=head;
 5         while(fast!=null && fast.next!=null){
 6             slow=slow.next;
 7             fast=fast.next.next;
 8         }
 9         Stack<ListNode> stack=new Stack<>();
10         ListNode half=slow.next;
11         while(half!=null){
12             stack.push(half);
13             half=half.next;
14         }
15         slow.next=null;
16         ListNode current=head;
17         while(current!=null && !stack.isEmpty()){
18             ListNode next=current.next;
19             current.next=stack.pop();
20             current.next.next=next;
21             current=next;
22         }
23     }
24 }

原文地址:https://www.cnblogs.com/kexinxin/p/10195954.html

时间: 2024-08-02 12:05:59

Leetcode 143.重排链表的相关文章

143. 重排链表

题目描述 给定一个单链表 L:L0→L1→-→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例 1: 给定链表 1->2->3->4, 重新排列为 1->4->2->3. 示例 2: 给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3. 分析 使用双向链表queue来存该单链表,再迭代一

Leetcode:Partition List 链表快速排序划分

Partition List Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given

Leetcode:Rotate List 链表旋转

Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 解题分析: 不同于数组旋转,数组可以随机存取,数组的旋转可以巧妙的 分成两两部分递归旋转 对于链表的旋转,实际上

Linked List Cycle leetcode java (链表检测环)

题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 题解: 这道题连带着II是很经典的,在看CC150时候,纠结这个问题纠结了很久.在读了很多网上资料还有书的讲解以及和别人讨论之后,对这个专题终于明白了. 这一问只需要判断链表是否有环. 当链表没有环时是很好判断的,让一个指针一直往后走,遇见null了自然就没有环. 而如

99. 重排链表

99. 重排链表 难度系数 中等 通过率 24% 描述 笔记 数据 评测 给定一个单链表L: L0→L1→-→Ln-1→Ln, 重新排列后为:L0→Ln→L1→Ln-1→L2→Ln-2→- 必须在不改变节点值的情况下进行原地操作. 您在真实的面试中是否遇到过这个题? Yes 样例 给出链表 1->2->3->4->null,重新排列后为1->4->2->3->null /** * Definition of ListNode * class ListNode

GPLT天梯赛 L2-022. 重排链表

L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2→4→3. 输入格式: 每个输入包含1个测试用例.每个测试用例第1行给出第1个结点的地址和结点总个数,即正整数N (<= 105).结点的地址是5位非负整数,NULL地址用-

图解精选 TOP 面试题 001 | LeetCode 237. 删除链表中的节点

题目描述 原题链接 LeetCode 237. 删除链表中的节点:https://leetcode-cn.com/problems/delete-node-in-a-linked-list 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 head =?[4,5,1,9],它可以表示为: 示例 1: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 ?5? 的第二个节点,那么在调

[leetcode]143. Reorder List重排链表

Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You may not modify the values in the list's nodes, only nodes itself may be changed. Example 1: Given 1->2->3->4, reorder it to 1->4->2->3. Example 2: G

LeetCode 143题:Reorder List的分析及两种解题思路

特别说明:参考了很多前辈的文章,整理如下,我只做了重新编码的工作,不能保证代码最优,主要用作交流学习. 题目: 方法1:将链表的每个节点地址保存在指针数组中,利用数组随机访问调整链表. 1 struct ListNode 2 { 3 int val; 4 ListNode *next; 5 ListNode(int x) : val(x), next(NULL) {} 6 }; 7 8 void reorderList(ListNode *head) 9 { 10 if (head == nul