【Java - J - 24】反转链表

题目描述

输入一个链表,反转链表后,输出新链表的表头。

lc
练习地址

0025】h - x - K 个一组翻转链表

实现

方法1:

/**
 * 反转链表
 * 定义一个方法(函数),
 * 实现输入一个链表的头结点,然后可以反转这个链表的方向,并输出反转之后的链表的头结点。
 * 输入: 1->2->3->4->5->NULL
 * 输出: 5->4->3->2->1->NULL
 */
public class C24_list_ReverseList {

    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
        }
    }
    static class Solution {
        public ListNode reverseList(ListNode head) {
            if (head == null) {
                return null;
            }
            if (head.next == null) {
                return head;
            }
            //p1、p2获取初始值
            ListNode p1 = head;
            ListNode p2 = head.next;
            p1.next = null;
            //遍历链表
            while (p2 != null) {
                ListNode tmp = p2.next; //存储
                p2.next = p1; //断开
                //开始移动
                p1 = p2;  //移动p1
                p2 = tmp; //移动p2
            }
            return p1;
        }
    }
}

方法2:递归

 /**
     * 递归
     * https://blog.csdn.net/SoulOH/article/details/81062223
     */
    class Solution2 {
        public ListNode reverseList(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode subListHead = reverseList(head.next);
            head.next.next = head;
            head.next = null;
            return subListHead;
        }
    }
Test
public static void main(String[] args) {
            ListNode node1 = new ListNode(1);
            ListNode node2 = new ListNode(2);
            ListNode node3 = new ListNode(3);
            node1.next = node2;
            node2.next = node3;

            ListNode head = node1;
            while (head != null) {
                System.out.println(head.val);
                head = head.next;
            }

            System.out.println("-----------");
            Solution solution = new Solution();
            ListNode res = solution.reverseList(node1);
            System.out.println("res:" + res.val);

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值