反转链表_206_92_25

LeetCode_206.反转链表:https://leetcode-cn.com/problems/reverse-linked-list/

题目描述:

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

复制代码
思路1:迭代实现
    就是从头开始遍历,在遍历链表的时候,将当前节点的next指针改为指向前一个节点。
    由于节点没有引用前一个节点,故需要先定义一个头节点prev,保证原来的链表第一个节点指向该节点。
    遍历的时候,需要保留下一个待遍历的节点。
    遍历的终止条件是:当前节点cur,不为空。
    
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode cur = head;
        while(cur != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }
}

思路2:递归
    递归的精髓在于,不要在意递归的过程,直接想递归的结果。
    就是假设链表的其余部分已经被反转
    比如,ListNode p = reverseList(head.next);  表示原链表的head.next部分已经完成了反转,
    现在应该如何反转它前面的部分?
    head.next = null;
    head.next.next = head;
    return p; 即可
    必须注意递归的终止条件:当head为空或者head.next为空的时候。
class Solution { public ListNode reverseList(ListNode head) { if (head == null || head.next == null) { return head; } ListNode newHead = reverseList(head.next); head.next.next = head; head.next = null; return newHead; } }
复制代码

LeetCode_92:反转链表的指定区间:https://leetcode-cn.com/problems/reverse-linked-list-ii/

题目描述:

  给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回反转后的链表 。
  

复制代码
    class Solution {
        public ListNode reverseBetween(ListNode head, int left, int right) {
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode p = dummy;
            ListNode q= dummy.next;
            for(int i=1; i<left; i++) {
                p = p.next;
                q = q.next;
            }
            for(int i=0; i<right-left; i++) {
                ListNode tmp = q.next;
                q.next = q.next.next;

                tmp.next = p.next;
                p.next = tmp;
            }
            return dummy.next;
        }
    }
复制代码

 LeetCode_25:k个一组反转链表:https://leetcode-cn.com/problems/reverse-nodes-in-k-group/

  给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。

  k 是一个正整数,它的值小于或等于链表的长度。

  如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。

复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if(head == null || head.next == null || k < 2) {
            return head;
        }
        ListNode dummy = new ListNode(0, head);
        ListNode prev = dummy;
        ListNode cur = head;
        while(cur != null) {
            int i = 1;
            ListNode end = cur;
            for(; i<k; i++) {
                if(end.next != null) {
                    end = end.next;
                } else {
                    return dummy.next;
                }
            }
            ListNode endNext = end.next;
            end.next = null;
            prev.next = reverse(cur);
            cur.next = endNext;
            prev = cur;
            cur = endNext;
        }
        return dummy.next;
    }

    public ListNode reverse(ListNode head) {
        ListNode prev = null;
        ListNode cur = head;
        while(cur != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }
}
复制代码

 

posted @   2022年总冠军gogogo  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示