LeetCode【328.奇偶链表】

版权声明: https://blog.csdn.net/qq_38386316/article/details/83152791

奇偶链表

给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的寄数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。

示例 1

  • 输入 1 > 2 > 3 > 4 > 5 > N U L L 1->2->3->4->5->NULL
  • 输出: 1 > 3 > 5 > 2 > 4 > N U L L 1->3->5->2->4->NULL

示例 2:

  • 输入: 2 > 1 > 3 > 5 > 6 > 4 > 7 > N U L L 2->1->3->5->6->4->7->NULL
  • 输出: 2 > 3 > 6 > 7 > 1 > 5 > 4 > N U L L 2->3->6->7->1->5->4->NULL

思路 1
首先,可以初始化两个节点,一个用来连接奇数节点,一个用来连接偶数节点(这个最后的指针需等于null),因为当链表中的节点数目是奇数个时,偶数节点的最后一个节点没有指向空,可能会造成循环链表;。

代码 1

class Solution {
    public ListNode oddEvenList(ListNode head) {
        ListNode OddNode=new ListNode(0);
        ListNode EvenNode=new ListNode(0); 
        ListNode cur=head,ptr=EvenNode,qtr=OddNode;//ptr 记录EvenNode初始位置,qtr 记录 OddNode初始位置
        while(cur!=null){                          //奇数给OddNode,偶数给EvenNode
             OddNode.next=cur;
             OddNode=OddNode.next;
             cur=cur.next;
             if(cur!=null){               
                 EvenNode.next=cur;
                 EvenNode=EvenNode.next;
                 cur=cur.next;
              }
        }
        EvenNode.next=null;
        OddNode.next=ptr.next;   //链接奇偶连表
        return qtr.next;
    }
}

复杂度分析

  • 时间复杂度 O ( N ) O(N)
  • 空间复杂度 O ( N ) O(N)

思路 2
直接在给出的链表上进行改动指针的指向,来达到题意的要求,奇数和偶数都是每隔一个节点,偶节点可以看作快指针,最后返回head.
代码 2

class Solution {
    public ListNode oddEvenList(ListNode head) {
       if(head == null || head.next == null) {
           return head;
       }
        ListNode odd = head, even = head.next, evenHead = even;
        while(even != null && even.next != null) {
            odd.next = odd.next.next;
            even.next = even.next.next;
            odd = odd.next;
            even = even.next;
        }
        odd.next = evenHead;
        return head;     
    }
}

复杂度分析

  • 时间复杂度 O ( N ) O(N)
  • 空间复杂度 O ( N ) O(N)

完整代码

package leetcode328;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by 张超帅 on 2018/10/19.
 */
class ListNode {
    int val;
    ListNode next;
    ListNode (int val){this.val = val;}
}
class Solution {
    public ListNode oddEvenList(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode odd = head, even = head.next, evenHead = even;
        while(even != null && even.next != null){
            odd.next = odd.next.next;
            even.next = even.next.next;
            odd = odd.next;
            even = even.next;
        }
        odd.next = evenHead;
        return head;
    }
}
public class leetcode328 {
    public static int[] stringToArrays(String input){
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if(input == null){
            return new int[0];
        }
        String[] parts = input.split(",");
        int[] res = new int[parts.length];
        for(int i = 0; i < parts.length; i ++){
            res[i] = Integer.parseInt(parts[i].trim());
        }
        return res;
    }
    public static ListNode stringToListNode(String input){
        int[] nodes = stringToArrays(input);
        ListNode cur = new ListNode(-1);
        ListNode dummpy = cur;
        for(int node : nodes){
            cur.next = new ListNode(node);
            cur = cur.next;
        }
        return dummpy.next;
    }
    public static String listnodeToString(ListNode head){
        if(head == null) {
            return "[]";
        }
        String res = "";
        while(head != null){
            res += head.val + ", ";
            head = head.next;
        }
        return "[" + res.substring(0, res.length() - 2) + "]";

    }
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in) );
        String line = null;
        while((line = in.readLine()) != null){
            ListNode head = stringToListNode(line);
            ListNode ret = new Solution().oddEvenList(head);
            System.out.println(listnodeToString(ret));
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38386316/article/details/83152791