• LeetCode反转链表的五种Java实现方式


    给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

    示例 1:

    输入:head = [1,2,3,4,5]
    输出:[5,4,3,2,1]
    

    示例 2:

    输入:head = [1,2]
    输出:[2,1]
    

    示例 3:

    输入:head = []
    输出:[]
    

    提示:

    • 链表中节点的数目范围是 [0, 5000]
    • -5000 <= Node.val <= 5000

    方法一

    创建一个新链表,遍历旧链表中的数据并将其放入新链表的头部。实现方式如下

    1. class Solution {
    2. public ListNode reverseList(ListNode head) {
    3. //创建一个新链表,将旧链表的头部放入新链表头部
    4. ListNode o1 = null;
    5. ListNode p = head;
    6. while(p!=null){
    7. o1=new ListNode(p.val,o1);
    8. p= p.next;
    9. }
    10. return o1;
    11. }
    12. }

    方法二

    构造一个新链表,从旧链表头部移除节点,添加到新链表头部。与方法一的区别在于,没有新创建节点。

    1. class Solution {
    2. public ListNode reverseList(ListNode head) {
    3. //旧节点
    4. Link o1 = new Link(head);
    5. Link o2 = new Link(null);
    6. while(true){
    7. ListNode first = o1.removeFirst();
    8. if(first==null){
    9. break;
    10. }
    11. o2.addFirst(first);
    12. }
    13. return o2.head;
    14. }
    15. static class Link{
    16. private ListNode head;
    17. public Link(ListNode head){
    18. this.head = head;
    19. }
    20. //传入新链表
    21. public void addFirst(ListNode first){
    22. first.next=head;
    23. head=first;
    24. }
    25. public ListNode removeFirst(){
    26. ListNode first =head;
    27. if(first!=null){
    28. //说明还存在节点
    29. head = head.next;
    30. }
    31. return first;
    32. }
    33. }
    34. }

    方法三

    采用递归思想,遍历到最后节点后,将最后一个节点指向上一个节点。

    图示如下:

    代码实现如下 

    1. class Solution {
    2. public ListNode reverseList(ListNode head) {
    3. if(head == null||head.next == null){
    4. return head;
    5. }
    6. //list作为头节点
    7. ListNode list = reverseList(head.next);
    8. //将当前节点作为list的最后一个节点
    9. head.next.next = head;
    10. //取消掉旧链表中的连接关系
    11. head.next =null;
    12. return list;
    13. }
    14. }

    方法四

    从链表每次拿到第二个节点,将其从链表断开,插入头部,直至它为 null 结束

    图示如下

    代码实现如下 

    1. class Solution {
    2. public ListNode reverseList(ListNode head) {
    3. if(head==null || head.next==null){
    4. return head;
    5. }
    6. ListNode poniter = head.next;
    7. //用来充当头节点
    8. ListNode p = head;
    9. //不停的将第二个节点插入该链表的头部
    10. while(poniter!=null){
    11. //头节点指向第三个节点
    12. head.next = poniter.next;
    13. //第二个节点更改为头节点
    14. poniter.next = p;
    15. //将p指向当前链表的头部
    16. p = poniter;
    17. //pointer指向移动后原链表的第二个节点
    18. poniter = head.next;
    19. }
    20. return p;
    21. }
    22. }

    方法五

    把链表分成两部分,思路就是不断从链表2的头,往链表1的头搬移

    图示如下:

    代码实现如下 

    1. class Solution {
    2. public ListNode reverseList(ListNode head) {
    3. if(head == null){
    4. return head;
    5. }
    6. ListNode p = null;
    7. while(head!=null){
    8. //第二个节点
    9. ListNode pointer = head.next;
    10. head.next = p;
    11. p = head;
    12. head = pointer;
    13. }
    14. return p;
    15. }
    16. }
  • 相关阅读:
    Response响应对象
    JavaWeb 项目 --- 博客系统(前后分离)
    ROS1余ROS2共存的一键安装(全)
    10、文本处理工具
    企业如何选择安全又稳定的文件传输协议
    实现两个div水平对齐
    【Python】解决类中特性(property)覆盖同名属性(attribute)报错问题
    14.Tensor Product:Covector-Covector Pairs
    CSS总结第六天
    效率翻倍:使用 ERP 系统自动执行这 5 项任务
  • 原文地址:https://blog.csdn.net/zmbwcx/article/details/134408021