• 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 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 1:

    Input: 1->2->3->4->5->NULL
    Output: 1->3->5->2->4->NULL
    

    Example 2:

    Input: 2->1->3->5->6->4->7->NULL
    Output: 2->3->6->7->1->5->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 ...

    two pointer

    time: O(n), space: O(1)

    /**
     * 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) {
                return head;
            }
            ListNode dummyOdd = new ListNode(0);
            ListNode dummyEven = new ListNode(0);
            ListNode curOdd = dummyOdd;
            ListNode curEven = dummyEven;
            
            ListNode cur = head;
            while(cur != null && cur.next != null) {
                curOdd.next = cur;
                curEven.next = cur.next;
                
                curOdd = curOdd.next;
                curEven = curEven.next;
                cur = cur.next.next;
            }
            if(cur != null) {
                curOdd.next = cur;
                curOdd.next.next = dummyEven.next;
            } else {
                curOdd.next = dummyEven.next;
            }
            
            curEven.next = null;
            return dummyOdd.next;
        }
    }

    optimized:

    /**
     * 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) {
                return head;
            }
            
            ListNode curOdd = head;
            ListNode evenHead = head.next;
            ListNode curEven = evenHead;
            
            while(curEven != null && curEven.next != null) {
                curOdd.next = curEven.next;
                curEven.next = curEven.next.next;
                
                curOdd = curOdd.next;
                curEven = curEven.next;
            }
            curOdd.next = evenHead;
            return head;
        }
    }
  • 相关阅读:
    01Servlet执行流程
    Tomcat配置虚拟主机
    Mac node 版本升级
    【微信小程序】【Demo】获取页面元素宽高,按相对百分比计算页面其他元素应设置宽度的像素数值。
    VM12解决出现与HyperV不兼容
    记录安装IDA插件Bindiff的坑
    C++ 正逆序迭代器
    智能指针虚析构函数基类指针访问子类方法
    编程最顶的八句格言
    visual studio C/C++ 创建使用动态库
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10184502.html
Copyright © 2020-2023  润新知