143:重排链表

This commit is contained in:
huangge1199 2021-06-07 10:37:09 +08:00
parent d0f4efa343
commit a6ff230d58
2 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,70 @@
//给定一个单链表 LL0L1Ln-1Ln
//将其重新排列后变为 L0LnL1Ln-1L2Ln-2
//
// 你不能只是单纯的改变节点内部的值而是需要实际的进行节点交换
//
// 示例 1:
//
// 给定链表 1->2->3->4, 重新排列为 1->4->2->3.
//
// 示例 2:
//
// 给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.
// Related Topics 链表
// 👍 593 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.ListNode;
import java.util.ArrayList;
import java.util.List;
//143:重排链表
public class ReorderList{
public static void main(String[] args) {
//测试代码
Solution solution = new ReorderList().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public void reorderList(ListNode head) {
if (head == null) {
return;
}
List<ListNode> list = new ArrayList<>();
ListNode temp = head.next;
while (temp != null) {
list.add(temp);
temp = temp.next;
}
temp = head;
int size = list.size();
int count = (size + 1) / 2;
for (int i = 1; i <= count; i++) {
if (size - i != i - 1) {
temp.next = list.get(size - i);
temp.next.next = list.get(i - 1);
temp = temp.next.next;
} else {
temp.next = list.get(i - 1);
temp = temp.next;
}
}
temp.next = null;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,13 @@
<p>给定一个单链表&nbsp;<em>L</em><em>L</em><sub>0</sub>&rarr;<em>L</em><sub>1</sub>&rarr;&hellip;&rarr;<em>L</em><sub><em>n</em>-1</sub>&rarr;<em>L</em><sub>n </sub><br>
将其重新排列后变为: <em>L</em><sub>0</sub>&rarr;<em>L</em><sub><em>n</em></sub>&rarr;<em>L</em><sub>1</sub>&rarr;<em>L</em><sub><em>n</em>-1</sub>&rarr;<em>L</em><sub>2</sub>&rarr;<em>L</em><sub><em>n</em>-2</sub>&rarr;&hellip;</p>
<p>你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。</p>
<p><strong>示例&nbsp;1:</strong></p>
<pre>给定链表 1-&gt;2-&gt;3-&gt;4, 重新排列为 1-&gt;4-&gt;2-&gt;3.</pre>
<p><strong>示例 2:</strong></p>
<pre>给定链表 1-&gt;2-&gt;3-&gt;4-&gt;5, 重新排列为 1-&gt;5-&gt;2-&gt;4-&gt;3.</pre>
<div><div>Related Topics</div><div><li>链表</li></div></div>\n<div><li>👍 593</li><li>👎 0</li></div>