反转单向链表和双向链表

描述

实现反转单向链表和双向链表的函数。

如 1->2->3 反转后变成 3->2->1。

示例

输入:

3
1 2 3
4
1 2 3 4

输出:

3 2 1
4 3 2 1

解题思路:(单向链表)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String[] n1=in.nextLine().split(" ");
        String[] num1=in.nextLine().split(" ");
        String[] n2=in.nextLine().split(" ");
        String[] num2=in.nextLine().split(" ");
        
        Node node=transForNode(num1);
        DoubleNode dnode=transForDoubleNode(num2);
        
        node=reverseList01(node);
        dnode=reverseList02(dnode);
        
        show01(node);
        show02(dnode);
    }
    //打印链表
    public static void show01(Node head){
        while(head!=null){
            System.out.print(head.value+" ");
            head=head.next;
        }
        System.out.println();
    }
    public static void show02(DoubleNode head){
        while(head!=null){
            System.out.print(head.value+" ");
            head=head.next;
        }
        System.out.println();
    }
    //数组转链表
    public static Node transForNode(String[] nums) {
        Node head = new Node(Integer.parseInt(nums[0]));
        Node cur = head;
        for(int i = 1; i < nums.length; i++) {
            cur.next = new Node(Integer.parseInt(nums[i]));
            cur = cur.next;
        }
        return head;
    }
    
    //数组转双向链表
    public static DoubleNode transForDoubleNode(String[] nums) {
        DoubleNode head = new DoubleNode(Integer.parseInt(nums[0]));
        DoubleNode cur = head;
        for(int i = 1; i < nums.length; i++) {
            DoubleNode nextDoubleNode = new DoubleNode(Integer.parseInt(nums[i]));
            cur.next=nextDoubleNode;
            nextDoubleNode.last=cur;
            cur = cur.next;
        }
        return head;
    }
    
    public static Node reverseList01(Node head){
        Node pre=null;
        Node next=null;
        while(head!=null){
            next=head.next;
            head.next=pre;
            pre=head;
            head=next;
        }
        return pre;
    }
    public static DoubleNode reverseList02(DoubleNode head){
        DoubleNode pre=null;
        DoubleNode next=null;
        while(head!=null){
            next=head.next;
            head.next=pre;
            head.last=next;
            pre=head;
            head=next;
        }
        return pre;
    }
}
class Node{
    public int value;
    public Node next;
    public Node(int value){
        this.value=value;
    }
}
class DoubleNode{
    public int value;
    public DoubleNode last;
    public DoubleNode next;
    public DoubleNode(int value){
        this.value=value;
    }
}

 

### Java 中单向链表双向链表反转实现 #### 单向链表反转 对于单向链表而言,其结构较为简单,仅包含指向下一个节点的指针。为了实现单向链表反转,可以通过迭代的方式逐步调整各节点之间的链接方向。 ```java public class SinglyLinkedList { public static ListNode reverseList(ListNode head) { ListNode prev = null; while (head != null) { ListNode nextNode = head.next; // 记录当前节点的下一节点 head.next = prev; // 将当前节点的next设为prev, 反转指针方向 prev = head; // 移动prev到当前节点位置 head = nextNode; // 继续处理下个节点 } return prev; // 返回新的头结点 } static class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } } ``` 此方法的时间复杂度为 O(n),其中 n 是列表中的元素数量[^1]。 #### 双向链表反转 双向链表由于存在前后两个指针,因此在执行反转操作时不仅需要改变 `next` 的指向还需要同步更新 `prev` 指针的方向: ```java public class DoublyLinkedList { public static Node reverse(Node node) { Node temp = null; Node current = node; while (current != null) { temp = current.prev; // 临时保存前驱节点 current.prev = current.next; // 前驱变为后继 current.next = temp; // 后继变为原来的前驱 current = current.prev; // 进入下一个待处理节点 } if(temp != null ) { node = temp.prev; // 更新头部指针至新起点 } return node; } static class Node { int data; Node next, prev; Node(int d) {data = d;} } } ``` 这段代码同样具有 O(n) 时间复杂度,并且通过交换每个节点上的 `next` `prev` 来完成整个过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值