LeetCode【328.奇偶链表】

本文介绍了一种算法,用于将单链表中的奇数节点和偶数节点分离并重新排列,通过两种方法实现:一是创建新的奇偶链表再连接,二是直接在原链表上修改指针。两种方法的时间复杂度均为O(N)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

奇偶链表

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

示例 1

  • 输入1−>2−>3−>4−>5−>NULL1->2->3->4->5->NULL1>2>3>4>5>NULL
  • 输出: 1−>3−>5−>2−>4−>NULL1->3->5->2->4->NULL1>3>5>2>4>NULL

示例 2:

  • 输入: 2−>1−>3−>5−>6−>4−>7−>NULL2->1->3->5->6->4->7->NULL2>1>3>5>6>4>7>NULL
  • 输出: 2−>3−>6−>7−>1−>5−>4−>NULL2->3->6->7->1->5->4->NULL2>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)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)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));
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值