328:奇偶链表

This commit is contained in:
huangge1199 2021-06-07 10:59:00 +08:00
parent ac30179a33
commit 8072745345
2 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,67 @@
//给定一个单链表把所有的奇数节点和偶数节点分别排在一起请注意这里的奇数节点和偶数节点指的是节点编号的奇偶性而不是节点的值的奇偶性
//
// 请尝试使用原地算法完成你的算法的空间复杂度应为 O(1)时间复杂度应为 O(nodes)nodes 为节点总数
//
// 示例 1:
//
// 输入: 1->2->3->4->5->NULL
//输出: 1->3->5->2->4->NULL
//
//
// 示例 2:
//
// 输入: 2->1->3->5->6->4->7->NULL
//输出: 2->3->6->7->1->5->4->NULL
//
// 说明:
//
//
// 应当保持奇数节点和偶数节点的相对顺序
// 链表的第一个节点视为奇数节点第二个节点视为偶数节点以此类推
//
// Related Topics 链表
// 👍 428 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.ListNode;
//328:奇偶链表
public class OddEvenLinkedList{
public static void main(String[] args) {
//测试代码
Solution solution = new OddEvenLinkedList().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 ListNode oddEvenList(ListNode head) {
if (head == null || head.next == null || head.next.next == null) {
return head;
}
ListNode odd = head;
ListNode even = head.next;
ListNode evenTemp = even;
while (even != null && even.next != null) {
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenTemp;
return head;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,22 @@
<p>给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。</p>
<p>请尝试使用原地算法完成。你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes)nodes 为节点总数。</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong> 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL
<strong>输出:</strong> 1-&gt;3-&gt;5-&gt;2-&gt;4-&gt;NULL
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong> 2-&gt;1-&gt;3-&gt;5-&gt;6-&gt;4-&gt;7-&gt;NULL
<strong>输出:</strong> 2-&gt;3-&gt;6-&gt;7-&gt;1-&gt;5-&gt;4-&gt;NULL</pre>
<p><strong>说明:</strong></p>
<ul>
<li>应当保持奇数节点和偶数节点的相对顺序。</li>
<li>链表的第一个节点视为奇数节点,第二个节点视为偶数节点,以此类推。</li>
</ul>
<div><div>Related Topics</div><div><li>链表</li></div></div>\n<div><li>👍 428</li><li>👎 0</li></div>