[leetcode]143. Reorder List重排链表

Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→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:

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

题意:

按照头尾交替的方式重排一个链表

思路:

使用快慢指针找到链表中点,并将链表从中点处断开,形成2个独立的链表

将第2个链表翻转

将第2个链表的元素间隔地插入第1个链表

代码:

 1 class Solution {
 2     public void reorderList(ListNode head){
 3         if (head == null || head.next == null) return;
 4         ListNode slow = head, fast = head, prev = null;
 5         while (fast != null && fast.next != null) {
 6             prev = slow;
 7             slow = slow.next;
 8             fast = fast.next.next;
 9         }
10         prev.next = null; // cut at middle
11
12         slow = reverse(slow);
13
14         // merge two lists
15         ListNode curr = head;
16         while (curr.next != null) {
17             ListNode tmp = curr.next;
18             curr.next = slow;
19             slow = slow.next;
20             curr.next.next = tmp;
21             curr = tmp;
22         }
23         curr.next = slow;
24     }
25
26     ListNode reverse(ListNode head) {
27         if (head == null || head.next == null) return head;
28         ListNode prev = head;
29         for (ListNode curr = head.next, next = curr.next; curr != null;
30             prev = curr, curr = next, next = next != null ? next.next : null) {
31                 curr.next = prev;
32         }
33         head.next = null;
34         return prev;
35     }
36 }

原文地址:https://www.cnblogs.com/liuliu5151/p/9227177.html

时间: 2024-08-02 12:06:00

[leetcode]143. Reorder List重排链表的相关文章

leetcode 143. Reorder List ----- java

Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 按照题意改变链表结构. 1.使用list记录链表. /** * Definition for si

Java for 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 must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. 解题思路一: 每次将Ln换到前面,得到L0→Ln→L1→L2→L3→,然后对L1使用相同操作,

【Leetcode解题报告】单链表结点位置调整

Leetcode 206 Reverse Linked List 题目描述 Reverse a singly linked list. 分析与解法 (1) 递归解法 参考代码如下: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { pu

Leetcode 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. 1 public class Solution{ 2

143. Reorder List - LeetCode

Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前后两部分,将后部分链表反转,再将两部分链表连接成一个新链表 Java实现: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x)

143. Reorder List(js)

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-&

leetcode——Insertion Sort List 对链表进行插入排序(AC)

Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode *result; result->val = INT_MIN; result->next = NULL; ListNode *cur=head,*

Leetcode:Swap Nodes in Pairs 链表成对交换节点

Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the va

Linked List Cycle leetcode II java (寻找链表环的入口)

题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 题解: 这个连同I都是很经典的题啦,刷CC150时候就折磨了半天. 其实就推几个递推公式就好..首先看图(图引用自CC150): 从链表起始处到环入口长度为:a,从环入口到Faster和Sl