【Leetcode】 Odd Even Linked List

题目链接:https://leetcode.com/problems/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 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 ...

思路:

用两个头结点 分别将奇、偶结点链接 形成两个链表  最后将两链表连接

算法:

public ListNode oddEvenList(ListNode head) {
    ListNode oddHead = new ListNode(-1);
    ListNode evenHead = new ListNode(-1);
    ListNode p = head, op = oddHead, ep = evenHead;
    int count = 0;
    while (p != null) {
        count++;
        if (count % 2 == 1) {
            op.next = p;
            op = p;
        } else {
            ep.next = p;
            ep = p;
        }
        p = p.next;
    }
    ep.next = null;
    op.next = evenHead.next;
    return oddHead.next;
}  
时间: 2024-08-29 01:26:10

【Leetcode】 Odd Even Linked List的相关文章

【链表】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 value in the nodes. You should try to do it in place. The program should run in O(1) space comp

【LeetCode】203. Remove Linked List Elements

题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 题解: 这道题没什么好讲的,基础操作.需要注意的是链表的第一个node,因为没有前驱节点,所以该node需

【LeetCode】234 - Palindrome Linked List

Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? Hide Tags: Linked List Two Pointers 1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 5 typedef struct ListNode

【leetcode】92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

【LeetCode】206 - Reverse Linked List

Reverse a singly linked list. Hint:A linked list can be reversed either iteratively or recursively. Could you implement both? Solution 1:iteration 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 *

【LeetCode】234. Palindrome Linked List (2 solutions)

Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? 解法一: 一次遍历,装入vector,然后再一次遍历判断回文. 时间复杂度O(n),空间复杂度O(n) /** * Definition for singly-linked list. * struct ListNode

【Leetcode】92. Reverse Linked List II &amp;&amp; 206. Reverse Linked List

The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three pointers to achieve this goal. 1) Pointer to last value 2) Pointer to cur p value 3) Pointer to next value Here, showing my code wishes can help u. #incl

【LeetCode】链表 linked list(共34题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 =

【LeetCode】Linked List Cycle

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 做完Linked List Cycle II在做这题简直就是阉割版.. fast每次前进两步,slow每次前进一步,如果相遇则为true,否则false class Solution { public: bool hasCycle(ListNo