成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode/LintCode] Odd Even Linked List

awokezhou / 1095人閱讀

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.

Example

Example:
Given 1->2->3->4->5->NULL,
return 1->3->5->2->4->NULL.

Solution
public class Solution {
    /**
     * @param head: a singly linked list
     * @return: Modified linked list
     */
    public ListNode oddEvenList(ListNode head) {
        if (head != null) {
        //odd and even as two pointers, head and evenHead as two heads
            ListNode odd = head;
            ListNode even = head.next;
            ListNode evenHead = head.next;
            while (even != null && even.next != null) {
                odd.next = even.next;
                even.next = even.next.next;
                odd = odd.next;
                even = even.next;
            }
            odd.next = evenHead;
        }

        return head;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/71608.html

相關(guān)文章

  • leetcode 328. Odd Even Linked List

    摘要:最后將奇數(shù)鏈表尾連到偶數(shù)鏈表頭即可。改進(jìn)的思路在于減少額外的變量創(chuàng)建。奇數(shù)指針的初始值為,而偶數(shù)指針的初始值為。則下一個(gè)奇數(shù)值位于上,此時(shí)將該奇數(shù)指針移動(dòng)到上之后,偶數(shù)指針的值則為。 題目要求 Given a singly linked list, group all odd nodes together followed by the even nodes. Please note...

    Vultr 評論0 收藏0
  • [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 ...

    Invoker 評論0 收藏0
  • LeetCode 328:奇偶鏈表 Odd Even Linked List

    摘要:給定一個(gè)單鏈表,把所有的奇數(shù)節(jié)點(diǎn)和偶數(shù)節(jié)點(diǎn)分別排在一起。鏈表的第一個(gè)節(jié)點(diǎn)視為奇數(shù)節(jié)點(diǎn),第二個(gè)節(jié)點(diǎn)視為偶數(shù)節(jié)點(diǎn),以此類推。需要記錄偶數(shù)位節(jié)點(diǎn)的第一個(gè)節(jié)點(diǎn),因?yàn)檫@是偶數(shù)鏈表的頭節(jié)點(diǎn),最后拼接鏈表時(shí)要用奇數(shù)鏈表的尾節(jié)點(diǎn)連接該節(jié)點(diǎn)。 ?給定一個(gè)單鏈表,把所有的奇數(shù)節(jié)點(diǎn)和偶數(shù)節(jié)點(diǎn)分別排在一起。請注意,這里的奇數(shù)節(jié)點(diǎn)和偶數(shù)節(jié)點(diǎn)指的是節(jié)點(diǎn)編號(hào)的奇偶性,而不是節(jié)點(diǎn)的值的奇偶性。 請嘗試使用原地算法完成...

    yeooo 評論0 收藏0
  • LeetCode 328:奇偶鏈表 Odd Even Linked List

    摘要:給定一個(gè)單鏈表,把所有的奇數(shù)節(jié)點(diǎn)和偶數(shù)節(jié)點(diǎn)分別排在一起。鏈表的第一個(gè)節(jié)點(diǎn)視為奇數(shù)節(jié)點(diǎn),第二個(gè)節(jié)點(diǎn)視為偶數(shù)節(jié)點(diǎn),以此類推。需要記錄偶數(shù)位節(jié)點(diǎn)的第一個(gè)節(jié)點(diǎn),因?yàn)檫@是偶數(shù)鏈表的頭節(jié)點(diǎn),最后拼接鏈表時(shí)要用奇數(shù)鏈表的尾節(jié)點(diǎn)連接該節(jié)點(diǎn)。 ?給定一個(gè)單鏈表,把所有的奇數(shù)節(jié)點(diǎn)和偶數(shù)節(jié)點(diǎn)分別排在一起。請注意,這里的奇數(shù)節(jié)點(diǎn)和偶數(shù)節(jié)點(diǎn)指的是節(jié)點(diǎn)編號(hào)的奇偶性,而不是節(jié)點(diǎn)的值的奇偶性。 請嘗試使用原地算法完成...

    flybywind 評論0 收藏0
  • 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 node number and notthe value in the nodes.You should try to do it in plac...

    abson 評論0 收藏0

發(fā)表評論

0條評論

最新活動(dòng)
閱讀需要支付1元查看
<