Leet Code OJ 328. Odd Even Linked List [Difficulty: Easy]

题目:

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 …

翻译:

给定一个单链表,把全部的奇数节点放到一起,全部的偶数节点放到单数节点的后面。请注意这里我们讨论的是节点的编号的奇数偶数,而不是节点的值。

你应该直接在链表上去做。这个程序应该执行在O(1)的空间复杂度和O(节点数)的时间复杂度。

提示:这些奇数偶数区域中相关联的前后顺序。应该和输入的时候一样。第一个节点作为奇数节点,第二个作为偶数节点,由此类推。

。。

分析:

本题比較easy出错的地方,第一个是看错题目。把编号的奇偶数看成节点值的奇偶数;第二个是移动节点时。对于next指针的处理上。假设没有把next正确指向,有可能把链表变成循环列表。这个时候就会出现“Memory Limit Exceeded”这种错误了。以下的代码创建了多个暂时变量,把奇数偶数节点分开为2个链表,最后再合并的做法。也是为了代码清晰易读,事实上是能够有更精简的写法的,欢迎读者补充。

代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode oddEvenList(ListNode head) {
        ListNode oddFirstNode = new ListNode(0);
        ListNode oddCurrentNode = oddFirstNode;
        ListNode evenFirstNode = new ListNode(0);
        ListNode evenCurrentNode = evenFirstNode;
        int count=1;
        while (head != null) {
            ListNode temp=head.next;
            if (count% 2 == 1) {
                oddCurrentNode.next=head;
                oddCurrentNode=head;
            } else {
                evenCurrentNode.next=head;
                evenCurrentNode=head;
            }
            head.next=null;
            head = temp;
            count++;
        }
        oddCurrentNode.next=evenFirstNode.next;
        return oddFirstNode.next;
    }
}
时间: 2024-08-25 08:01:27

Leet Code OJ 328. Odd Even Linked List [Difficulty: Easy]的相关文章

Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 翻译: 给定一个下标k,返回第k行的杨辉三角. 例如给定k=3,返回[1,3,3,1]. 提示:你可以优化你的算法,让它只使用O(k)的额

Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]

题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 翻译: 合并2个已经排序的链表,并且返回一个新的链表.这个新的链表应该由前面提到的2个链表的节点所组成. 分析: 注意头节点的处理,和链表结束(next为null)的处理.以下代码新增了一个头指针,来把头节点

Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]

题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be

Leet Code OJ 58. Length of Last Word [Difficulty: Easy]

题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]

题目: Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 翻译: 给定一个正数,返回它类似Excle中对应的列标题. 分析: 关联问题:"Excel Sheet Column Number"

<LeetCode OJ> 328. Odd Even Linked List

328. Odd Even Linked List My Submissions Question Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node numbe

<LeetCode OJ> 328. Odd Even Linked List

328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy 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 t

Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 翻译: 给定一个数numRows,产生前numRows行的杨辉三角(即贾宪三角形.帕斯卡三角形). 分析: 除了每行首尾是1以外,其他元素均可由上行推出,本方案采用lastLine保存上行数

[LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)

每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝试使用原地算法完成.你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数. 示例 1: 输入: 1->2->3->4->5->NULL  输出: 1->3->5->2->4->NULL 示例 2: 输入: 2-