国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

LeetCode //C - 328. Odd Even Linked List

這篇具有很好參考價(jià)值的文章主要介紹了LeetCode //C - 328. Odd Even Linked List。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

328. Odd Even Linked List

Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.

The first node is considered odd, and the second node is even, and so on.

Note that the relative order inside both the even and odd groups should remain as it was in the input.

You must solve the problem in O(1) extra space complexity and O(n) time complexity.
?

Example 1:

LeetCode //C - 328. Odd Even Linked List,LeetCode,leetcode,c語(yǔ)言,算法

Input head = [1,2,3,4,5]
Output [1,3,5,2,4]

Example 2:

LeetCode //C - 328. Odd Even Linked List,LeetCode,leetcode,c語(yǔ)言,算法

Input head = [2,1,3,5,6,4,7]
Output [2,3,6,7,1,5,4]

Constraints:
  • The number of nodes in the linked list is in the range [ 0 , 1 0 4 ] [0, 10^4] [0,104].
  • ? 1 0 6 < = N o d e . v a l < = 1 0 6 -10^6 <= Node.val <= 10^6 ?106<=Node.val<=106

From: LeetCode
Link: 328. Odd Even Linked List


Solution:

Ideas:

This function works by first checking if the head is NULL. If it’s not, it creates two pointers, odd and even, which point to the first and second nodes of the list, respectively. evenHead stores the head of the even list.

The loop continues until there are no more even nodes or even nodes with a next node. Inside the loop, the odd nodes are connected to the next odd node, and similarly, the even nodes are connected to the next even node. After the end of the loop, the odd list and the even list are connected.

The final list starts with the odd nodes followed by the even nodes, as shown in your images.文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-822301.html

Code:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* oddEvenList(struct ListNode* head) {
    if (head == NULL) return NULL;
    
    struct ListNode *odd = head;
    struct ListNode *even = head->next;
    struct ListNode *evenHead = even;

    while (even != NULL && even->next != NULL) {
        odd->next = odd->next->next;
        even->next = even->next->next;
        odd = odd->next;
        even = even->next;
    }
    
    odd->next = evenHead;
    return head;
}

到了這里,關(guān)于LeetCode //C - 328. Odd Even Linked List的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【set】個(gè)人練習(xí)-Leetcode-817. Linked List Components

    題目鏈接:https://leetcode.cn/problems/linked-list-components/description/ 題目大意:給出一個(gè) vectorint nums ,其中有一些數(shù)字。再給出一個(gè)鏈表的頭指針 head ,鏈表內(nèi)的元素各不相同。如果鏈表中有某一段(長(zhǎng)度大于等于1)的元素都在 nums 中出現(xiàn)過,那么就算一個(gè)component,求鏈表中的co

    2024年02月13日
    瀏覽(22)
  • LeetCode 92. Reverse Linked List II【鏈表,頭插法】中等

    LeetCode 92. Reverse Linked List II【鏈表,頭插法】中等

    本文屬于「征服LeetCode」系列文章之一,這一系列正式開始于2021/08/12。由于LeetCode上部分題目有鎖,本系列將至少持續(xù)到刷完所有無(wú)鎖題之日為止;由于LeetCode還在不斷地創(chuàng)建新題,本系列的終止日期可能是永遠(yuǎn)。在這一系列刷題文章中,我不僅會(huì)講解多種解題思路及其優(yōu)化,

    2024年02月09日
    瀏覽(19)
  • 【LeetCode 算法】Linked List Cycle II 環(huán)形鏈表 II

    給定一個(gè)鏈表的頭節(jié)點(diǎn) head ,返回鏈表開始入環(huán)的第一個(gè)節(jié)點(diǎn)。 如果鏈表無(wú)環(huán),則返回 null。 如果鏈表中有某個(gè)節(jié)點(diǎn),可以通過連續(xù)跟蹤 next 指針再次到達(dá),則鏈表中存在環(huán)。 為了表示給定鏈表中的環(huán),評(píng)測(cè)系統(tǒng)內(nèi)部使用整數(shù) pos 來(lái)表示鏈表尾連接到鏈表中的位置(索引從

    2024年02月14日
    瀏覽(19)
  • Leetcode 1367. Linked List in Binary Tree (二叉樹好題)

    Linked List in Binary Tree Medium Given a binary tree root and a linked list with head as the first node. Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False. In this context downward path means a path that starts at some node and goes downwards. Exampl

    2024年01月25日
    瀏覽(22)
  • LeetCode //C - 114. Flatten Binary Tree to Linked List

    LeetCode //C - 114. Flatten Binary Tree to Linked List

    Given the root of a binary tree, flatten the tree into a “l(fā)inked list”: The “l(fā)inked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null. The “l(fā)inked list” should be in the same order as a pre-order traversal of the binary tree. ? Example 1: Input: ro

    2024年02月09日
    瀏覽(20)
  • LeetCode //C - 2130. Maximum Twin Sum of a Linked List

    LeetCode //C - 2130. Maximum Twin Sum of a Linked List

    In a linked list of size n, where n is even, the i t h i^{th} i t h node (0-indexed) of the linked list is known as the twin of the ( n ? 1 ? i ) t h (n-1-i)^{th} ( n ? 1 ? i ) t h node, if 0 = i = (n / 2) - 1. For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4. Th

    2024年01月17日
    瀏覽(85)
  • LeetCode 1019. Next Greater Node In Linked List【單調(diào)棧模板】中等

    本文屬于「征服LeetCode」系列文章之一,這一系列正式開始于2021/08/12。由于LeetCode上部分題目有鎖,本系列將至少持續(xù)到刷完所有無(wú)鎖題之日為止;由于LeetCode還在不斷地創(chuàng)建新題,本系列的終止日期可能是永遠(yuǎn)。在這一系列刷題文章中,我不僅會(huì)講解多種解題思路及其優(yōu)化,

    2023年04月18日
    瀏覽(16)
  • LeetCode //C - 2095. Delete the Middle Node of a Linked List

    LeetCode //C - 2095. Delete the Middle Node of a Linked List

    You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list. The middle node of a linked list of size n is the [ n / 2 ] t h [n / 2]^{th} [ n /2 ] t h node from the start using 0-based indexing, where ?x? denotes the largest integer less than or equal to x. For n = 1, 2, 3, 4, and 5, the middle nodes a

    2024年02月02日
    瀏覽(44)
  • 【LeetCode】328. 奇偶鏈表

    【LeetCode】328. 奇偶鏈表

    思路 如果鏈表為空,則直接返回鏈表。 對(duì)于原始鏈表,每個(gè)節(jié)點(diǎn)都是奇數(shù)節(jié)點(diǎn)或偶數(shù)節(jié)點(diǎn)。頭節(jié)點(diǎn)是奇數(shù)節(jié)點(diǎn),頭節(jié)點(diǎn)的后一個(gè)節(jié)點(diǎn)是偶數(shù)節(jié)點(diǎn),相鄰節(jié)點(diǎn)的奇偶性不同。因此可以將奇數(shù)節(jié)點(diǎn)和偶數(shù)節(jié)點(diǎn)分離成奇數(shù)鏈表和偶數(shù)鏈表,然后將偶數(shù)鏈表連接在奇數(shù)鏈表之后,合

    2024年02月10日
    瀏覽(14)
  • leetcode-328 奇偶鏈表

    leetcode-328 奇偶鏈表

    題目如下: 給定單鏈表的頭節(jié)點(diǎn) head ,將所有索引為奇數(shù)的節(jié)點(diǎn)和索引為偶數(shù)的節(jié)點(diǎn)分別組合在一起,然后返回重新排序的列表。 第一個(gè)節(jié)點(diǎn)的索引被認(rèn)為是 奇數(shù) , 第二個(gè)節(jié)點(diǎn)的索引為 偶數(shù) ,以此類推。 請(qǐng)注意,偶數(shù)組和奇數(shù)組內(nèi)部的相對(duì)順序應(yīng)該與輸入時(shí)保持一致。

    2024年02月01日
    瀏覽(14)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包