LeetCode 328. Odd Even Linked List

原创 2018年04月17日 09:55:31

问题描述

  • 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 …
  • 地址

问题分析

  • 做法类似于LeetCode 86. Partition List同样是分成两部分(虚拟出两条链表),同样是保持原来顺序(从前到后遍历),同样是 in place 。

代码实现

  • 自己
/**
 * 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 oddHead = head;
        ListNode oddTail = oddHead;
        ListNode evenHead = head.next;
        ListNode evenTail = evenHead;
        ListNode curNode = head.next.next;
        boolean isOdd = true;
        while (curNode != null) {
            if (isOdd) {
                oddTail.next = curNode;
                oddTail = curNode;
            }else {
                evenTail.next = curNode;
                evenTail = curNode;
            }
            isOdd = ! isOdd;
            curNode = curNode.next;
        }
        oddTail.next = evenHead;
        evenTail.next = null;
        return head;
    }
}
public class Solution {
    public ListNode oddEvenList(ListNode head) {
        if (head == null) return null;
        ListNode odd = head, even = head.next, evenHead = even;
        while (even != null && even.next != null) {
            odd.next = even.next;
            odd = odd.next;
            even.next = odd.next;
            even = even.next;
        }
        odd.next = evenHead;
        return head;
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjxxyz123/article/details/79970408

[leetcode-328]Odd Even Linked List(java)

问题描述:这里写链接内容问题分析:这里就是利用奇数和偶数位之间相互错位的位置关系。/** * Definition for singly-linked list. * public class L...
  • zdavb
  • zdavb
  • 2016-01-19 20:46:46
  • 1562

【LEETCODE】328- Odd Even Linked List [Python]

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her...
  • aliceyangxi1987
  • aliceyangxi1987
  • 2016-02-23 11:37:21
  • 624

[leetcode] - 328. Odd Even Linked List

题意是要将所有序号为奇数的节点全部放在链表前面,序号为偶数的连在最后一个奇数节点后面,并且不能打乱顺序。主要序号定义为从节点头顺序编号,不是指节点的值。这道题略坑,本想着可以直接ac的,代码也比较优雅...
  • decaywood
  • decaywood
  • 2016-01-26 21:18:59
  • 429

<LeetCode OJ> 328. Odd Even Linked List

328. Odd Even Linked List My Submissions Question Total Accepted: 9271 Total Submissions: 24497 ...
  • EbowTang
  • EbowTang
  • 2016-01-30 16:38:44
  • 1357

LeetCode刷题 (Python) | 328. Odd Even Linked List

LeetCode刷题 (Python) | 328. Odd Even Linked List
  • Titan0427
  • Titan0427
  • 2016-02-10 22:07:31
  • 470

(LeetCode)Odd Even Linked List --- 奇偶链表

(LeetCode)Odd Even Linked List --- 奇偶链表
  • u012965373
  • u012965373
  • 2016-01-19 21:35:10
  • 2575

[C语言][LeetCode][328]Odd Even Linked List

题目Odd Even Linked List Given a singly linked list, group all odd nodes together followed by the eve...
  • Timsley
  • Timsley
  • 2016-03-08 12:17:00
  • 803

leetcode:Odd Even Linked List 【Java】

一、问题描述 Given a singly linked list, group all odd nodes together followed by the even nodes. Ple...
  • tterminator
  • tterminator
  • 2016-03-10 20:55:24
  • 245

leetCode 328. Odd Even Linked List

题目链接:         题目内容:         题目分析:
  • shen_jz2012
  • shen_jz2012
  • 2016-02-18 20:42:29
  • 325

LeetCode刷题(C++)——Odd Even Linked List(Medium)

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her...
  • YF_Li123
  • YF_Li123
  • 2017-05-19 10:53:46
  • 127
收藏助手
不良信息举报
您举报文章:LeetCode 328. Odd Even Linked List
举报原因:
原因补充:

(最多只允许输入30个字)