PTA L2-022 重排链表

给定一个单链表 L1​→L2​→⋯→Ln−1​→Ln​,请编写程序将链表重新排列为 Ln​→L1​→Ln−1​→L2​→⋯。例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2→4→3。

输入格式:

每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址和结点总个数,即正整数N (≤105)。结点的地址是5位非负整数,NULL地址用−1表示。

接下来有N行,每行格式为:

Address Data Next

其中Address是结点地址;Data是该结点保存的数据,为不超过105的正整数;Next是下一结点的地址。题目保证给出的链表上至少有两个结点。

输出格式:

对每个测试用例,顺序输出重排后的结果链表,其上每个结点占一行,格式与输入相同。

输入样例:

00100 6
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

输出样例:

68237 6 00100
00100 1 99999
99999 5 12309
12309 2 00000
00000 4 33218
33218 3 -1

做法:

测试点3的坑点:有多余节点,也就是有节点不在链表上。

1.根据题目信息建立单链表

2.将单链表扩充成双向链表

3.按题目要求输出结果。

注:输出时,先输出尾,再输出头,即是题目要求的输出。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 100010;

int e[N],l[N],r[N],id;

int main()
{
    int head = 0,n = 0;
    scanf("%d%d",&head,&n);
    for(int i = 0;i < n;i++)
    {
        int address = 0,data = 0,next = 0;
        scanf("%d%d%d",&address,&data,&next);
        e[address] = data;r[address] = next;
    }
    l[head]  = -1;
    int back = head;//建立双向链表
    for(int i = r[head],j = head,cnt = 0;i != -1;i = r[i],j = r[j])
    {
        l[i] = j;
        if(r[i] == -1) back = i;
        n = ++cnt;
    }
    n++;//测试点3的坑,有多余的节点。
    // cout << head << " " << back << endl;
    for(int i = 0,h = head,b = back;i < n;i ++)//h头指针,b尾指针
    {
        if(i % 2 == 0) 
        {
            printf("%05d %d ",b,e[b]);
            if(i == n - 1)puts("-1");
            else printf("%05d\n",h);
            b = l[b];
        }
        else 
        {
            printf("%05d %d ",h,e[h]);
            if(i == n - 1) puts("-1");
            else printf("%05d\n",b);
            h = r[h];
        }
    }
    return 0;
}

结果: 

### L2-022 重排链表 Java 实现 为了实现给定链表重新排列,可以采用双指针法来找到中间节点并反转后半部分链表。之后再将两个子链表交替合并。 #### 定义链表结构 首先定义单向链表数据结构: ```java class ListNode { int val; ListNode next; public ListNode(int x) { this.val = x; this.next = null; } } ``` #### 找到中间结点 通过快慢指针的方法寻找链表的中间位置,并将其分为两部分[^1]。 ```java private static ListNode findMiddleNode(ListNode head) { if (head == null || head.next == null) return head; ListNode slow = head, fast = head; while (fast != null && fast.next != null) { System.out.println("pre " + slow.val + " " + fast.val); slow = slow.next; fast = fast.next.next; } return slow; } ``` #### 反转链表 对于分割后的右半边链表执行翻转操作以便后续连接时能够按照题目要求顺序相连[^3]。 ```java public static ListNode reverseList(ListNode head) { ListNode prev = null; ListNode curr = head; while (curr != null) { ListNode tempNext = curr.next; curr.next = prev; prev = curr; curr = tempNext; } return prev; } ``` #### 合并链表 最后一步就是把前一半和经过逆转处理过的另一半依次交叉拼接起来形成最终结果。 ```java public void reorderList(ListNode head) { if(head==null||head.next==null){ return ; } // Find the middle of linked list [Problem 876] ListNode mid=findMiddleNode(head); // Reverse the second part of the list [Problem 206] ListNode l1=head,l2=mid.next; mid.next=null; l2=reverseList(l2); // Merge two sorted linked lists [Problem 21] mergeTwoLists(l1,l2); } // Helper function to interweave nodes from both halves. private void mergeTwoLists(ListNode l1,ListNode l2){ while(l1!=null&&l2!=null){ ListNode tmp1=l1.next,tmp2=l2.next; l1.next=l2;l2.next=tmp1; l1=tmp1;l2=tmp2; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值