PAT L2-022 重排链表

本文介绍了一种将链表L1→L2→...→Ln重新排列为Ln→L1→Ln-1→L2...的算法实现。通过示例说明了如何将链表1→2→3→4→5→6重排为6→1→5→2→4→3。文章提供了完整的C++代码实现,并解释了输入输出格式。

https://pintia.cn/problem-sets/994805046380707840/problems/994805057860517888

 

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

输入格式:

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

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

Address Data Next

其中Address是结点地址;Data是该结点保存的数据,为不超过1的正整数;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

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10;
int st, N;

struct Node{
    int nx;
    int address;
    int val;
}node[maxn];

vector<Node> line;
map<int, int> mp;
vector<Node> ans;

int main() {
    scanf("%d%d", &st, &N);
    Node n;
    for(int i = 0; i < N; i ++) {
        scanf("%d%d%d", &n.address, &n.val, &n.nx);
        node[n.address] = n;
    }

    int last = st;
    while(last != -1) {
        line.push_back(node[last]);
        last = node[last].nx;
    }

    int cnt = line.size();
    for(int i = cnt - 1; i >= (cnt - cnt / 2); i --) {
        printf("%05d %d %05d\n", line[i].address, line[i].val, line[cnt - 1 - i].address);
        if(cnt - 1 - i == (cnt - 1) / 2)
            printf("%05d %d -1\n", line[cnt - 1 - i].address, line[cnt - 1 - i].val);
        else
            printf("%05d %d %05d\n", line[cnt - 1 - i].address, line[cnt - i - 1].val, line[i - 1].address);
    }
    if(cnt % 2) printf("%05d %d -1\n", line[(cnt / 2)].address, line[(cnt / 2)].val);
    return 0;
}

  

转载于:https://www.cnblogs.com/zlrrrr/p/10483526.html

### 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; } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值