328. Odd Even Linked List

原创 2016年08月30日 08:01:13

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 …

Credits:
Special thanks to @DjangoUnchained for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

题目意思是说:将奇数节点和偶数节点取出来,然后奇数节点组成一条链表,偶数节点组成一条链表,最后将奇数节点放在偶数节点链表前面。

/**
 * 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) {

        if(head == null){

            return null;
        }else if(head.next ==null){
            return head;
        }

        ListNode odd = new ListNode(0);
        ListNode oddCurr = odd;
        ListNode even = new ListNode(0);
        ListNode evenCurr = even;
        int counter = 0;
        ListNode tmp = head;
        while(tmp != null){

            counter ++;
            if(counter % 2 == 1){

                oddCurr.next = new ListNode(tmp.val);
                oddCurr = oddCurr.next;
            }else{

                evenCurr.next = new ListNode(tmp.val);
                evenCurr = evenCurr.next;
            }
            tmp = tmp.next;
        }

        oddCurr.next = even.next;

        return odd.next;

    }
}

《C++primer(第五版)》学习之路-第九章:顺序容器

【 声明:版权所有,转载请标明出处,请勿用于商业用途。  联系信箱:libin493073668@sina.com】 9.1 顺序容器概述 1.顺序容器类型 vector          可...
  • libin1105
  • libin1105
  • 2015年09月23日 22:37
  • 1423

串口通信校验方式(even,odd,space,mark)

无校验 (no parity) 奇校验 (odd parity):如果字符数据位中"1"的数目是偶数,校验位为"1",如果"1"的数目是奇数,校验位应为"0"。(校验位调整个数) 偶校验 (ev...
  • u010963246
  • u010963246
  • 2015年07月31日 16:01
  • 7966

JSP中的EL表达式详细介绍

一、JSP EL语言定义          EL 提供了在 JSP 脚本编制元素范围外使用运行时表达式的功能。脚本编制元素是指页面中能够用于在 JSP 文件中嵌入 Java 代码的元素。它...
  • qq_26676207
  • qq_26676207
  • 2016年08月31日 14:53
  • 2658

leetcode328. Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her...
  • fanyanqin65
  • fanyanqin65
  • 2016年02月25日 10:10
  • 250

LeetCode笔记:328. Odd Even Linked List

将链表中奇偶位置的节点集中放置
  • Cloudox_
  • Cloudox_
  • 2017年03月30日 10:07
  • 481

<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
  • 1286

LeetCode 328. Odd Even Linked List 解题笔记

1.题目描述https://leetcode.com/problems/odd-even-linked-list/#/description Given a singly linked list,...
  • zhyh1435589631
  • zhyh1435589631
  • 2017年03月27日 21:28
  • 374

328. Odd Even Linked List

leetcode 328 - Easy
  • u010142706
  • u010142706
  • 2016年02月24日 21:13
  • 95

leetcode-328-Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her...
  • u014705854
  • u014705854
  • 2016年03月07日 22:46
  • 184

(M)328. Odd Even Linked List

链接:https://leetcode.com/problems/odd-even-linked-list/ 虽然标着Medium,但是还是马上就得出思路了。提交以后看了看标准答案,哎哟,自己的答案...
  • lolipusher
  • lolipusher
  • 2016年10月25日 17:02
  • 90
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:328. Odd Even Linked List
举报原因:
原因补充:

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