【LeetCode】328. Odd Even Linked List
Problem:
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.
You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.
Example:
Given 1->2->3->4->5->NULL
return
1->3->5->2->4->NULL
.Note:
The relative order inside both the even and odd groups should remain as it was in the input.
The first node is considered odd, the second node even and so on ...
題目:將原連結串列奇數位移動到偶數位前,(注意不是節點值奇偶)原來奇偶序列中順序不變。
思路:這個在迴圈中每次移動兩位,第一位為奇數位,第二位為偶數位,分別放進奇偶連結串列指標末尾。
程式碼:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ 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 cur = even; //head=head.next; ListNode node = head.next.next; while(node!=null){ odd.next=node; node = node.next; odd = odd.next; if(node==null) break; cur.next=node; node = node.next; cur=cur.next; } odd.next=even; cur.next=null; return head; } }
相關推薦
【Leetcode】 328. Odd Even Linked List
直接 ++ next rem dev rgs pac groups [] Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her
【LeetCode】328. Odd Even Linked List
Problem:Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the nod
白菜刷LeetCode記-328. Odd Even Linked List
null 位置 leetcode 全部 png bsp col fun 解決 發現簡單題越來越少了,想偷懶都不可以了,今天的題目是中等難度的題目,題目如下: 這個題目是要根據鏈表的位置來修改鏈表,位置為奇數的節點全部排到前面,位置為偶數的節點全部排到奇數的後面,並
leetcode:(328) Odd Even Linked List (java)
package LeetCode_LinkedList; /** * 題目: * Given a singly linked list, group all odd nodes together followed by the even nodes. * Please n
328. Odd Even Linked List
desc title oge this program django pub sin cnblogs Given a singly linked list, group all odd nodes together followed by the even
328 Odd Even Linked List 奇偶鏈表
for return ive listnode oge main rip pre all Given a singly linked list, group all odd nodes together followed by the even nodes. Please
328. Odd Even Linked List(連結串列)
https://leetcode.com/problems/odd-even-linked-list/description/ 題目:將連結串列奇數位上的節點放到偶數位上的節點前面。 思路:分別構造兩條連結串列:奇數位的連結串列,偶數位的連結串列,最後一個奇數位節點的下一個節點為
【LeetCode】【328】【Odd Even Linked List】【連結串列】
題目:Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the n
LeetCode 328. 奇偶連結串列(Odd Even Linked List)
題目描述 給定一個單鏈表,把所有的奇數節點和偶數節點分別排在一起。請注意,這裡的奇數節點和偶數節點指的是節點編號的奇偶性,而不是節點的值的奇偶性。 請嘗試使用原地演算法完成。你的演算法的空間複雜度應為 O(1),時間複雜度應為 O(nodes),nodes 為節點總數。 示例1
LeetCode 328:奇偶連結串列 Odd Even Linked List
給定一個單鏈表,把所有的奇數節點和偶數節點分別排在一起。請注意,這裡的奇數節點和偶數節點指的是節點編號的奇偶性,而不是節點的值的
[LeetCode] Odd Even Linked List 奇偶連結串列
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the va
LC_328. Odd Even Linked List
color not 指向 oge pre class urn node ide https://leetcode.com/problems/odd-even-linked-list/description/Given a singly linked list, group
【Leetcode】 328. 奇偶連結串列
給定一個單鏈表,把所有的奇數節點和偶數節點分別排在一起。請注意,這裡的奇數節點和偶數節點指的是節點編號的奇偶性,而不是節點的值的奇偶性。 請嘗試使用原地演算法完成。你的演算法的空間複雜度應為 O(1),時間複雜度應為 O(nodes),nodes 為節點總數。 示例
[Swift]LeetCode328. 奇偶連結串列 | Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the valu
【Leetcode】237. Delete Node in a Linked List
ext cti des tail tip body tno strong clas Write a function to delete a node (except the tail) in a singly linked list, given only acces
【leetcode】Flatten Binary Tree to Linked List
模式 分析 事情 oot left stat log 這樣的 要求 分析: 問題是將給定的二叉樹變換成令一種形式,這樣的類型的問題。其模式是,將左子樹變換成某種形式
【Leetcode】203. Remove Linked List Elements
tno lee div node move nod public amp ret Remove all elements from a linked list of integers that have value val. Example Given: 1 -->
【LeetCode】連結串列 linked list(共34題)
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } 【2】Add Two Numbers 【19】Remove Nth Node From End of List (2018年10月30日
【LeetCode】No.876 Middle of the Linked List
題目: Given a non-empty, singly linked list with head node head, return a middle node of linked list. If there are two middle nodes, return
【LeetCode】Linked List Cycle II(環形連結串列 II)
這是LeetCode裡的第142道題。 題目要求: 給定一個連結串列,返回連結串列開始入環的第一個節點。 如果連結串列無環,則返回 null。 說明:不允許修改給定的連結串列。 進階:你是否可以不用額外空間解決此題? 起初我在做這道題的時候,