LeetCode //C - 92. Reverse Linked List II

这篇具有很好参考价值的文章主要介绍了LeetCode //C - 92. Reverse Linked List II。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

92. Reverse Linked List II

Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
 

Example 1:

LeetCode //C - 92. Reverse Linked List II,LeetCode,leetcode,c语言,算法

Input: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]

Example 2:

Input: head = [5], left = 1, right = 1
Output: [5]

Constraints:
  • The number of nodes in the list is n.
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

From: LeetCode
Link: 92. Reverse Linked List II


Solution:

Ideas:

1. Handle Edge Cases:

  • If the head node is NULL or left is equal to right (meaning there’s no need to reverse anything), we immediately return the head.

2. Initialize Dummy Node:

  • We initialize a dummy node and set its next to head. This is a common trick to simplify code that changes the head node.

3. Move to left-th Node:

  • We move the prev pointer to the node immediately before the left-th node. The for-loop helps us move it.

4. Initialize cur and next Pointers:

  • cur is initialized to the left-th node (the node immediately after prev).
  • next is initialized to NULL.

5. Reverse Segment:

  • We reverse the segment of the linked list between the left-th and right-th nodes. We do this by iteratively moving nodes from the position immediately after cur to the position immediately after prev.

6. Return New Head:

  • Finally, we return the new head of the linked list, which is the node immediately after dummy.

By following these steps, we can reverse the segment of the linked list between the left-th and right-th nodes while keeping the rest of the list intact.文章来源地址https://www.toymoban.com/news/detail-679594.html

Code:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseBetween(struct ListNode* head, int left, int right) {
    if (head == NULL || left == right) {
        return head;
    }
    
    struct ListNode *dummy = (struct ListNode*) malloc(sizeof(struct ListNode));
    dummy->val = 0;
    dummy->next = head;
    
    struct ListNode *prev = dummy;
    
    // Move prev pointer to the node before the left-th node
    for (int i = 0; i < left - 1; ++i) {
        prev = prev->next;
    }
    
    struct ListNode *cur = prev->next;
    struct ListNode *next = NULL;
    
    // Reverse the nodes between left and right
    for (int i = 0; i < right - left; ++i) {
        next = cur->next;
        cur->next = next->next;
        next->next = prev->next;
        prev->next = next;
    }
    
    return dummy->next;
}

到了这里,关于LeetCode //C - 92. Reverse Linked List II的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • leetcode92 反转链表II

    题目 给你单链表的头指针 head 和两个整数 left 和 right ,其中 left = right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。 示例 输入:head = [1,2,3,4,5], left = 2, right = 4 输出:[1,4,3,2,5] 解析 这道题不太简单,分为两种方法,先说不好理解的那种方法,主要是

    2024年02月07日
    浏览(39)
  • leetcode做题笔记92. 反转链表 II

    给你单链表的头指针  head  和两个整数  left  和  right  ,其中  left = right  。请你反转从位置  left  到位置  right  的链表节点,返回  反转后的链表  。 示例 1: 本题根据left值将指针移动到目标节点前一位,再通过头插法将节点反转,最后返回链表 本题考察链表的应用

    2024年02月12日
    浏览(49)
  • 算法leetcode|92. 反转链表 II(rust重拳出击)

    给你单链表的头指针 head 和两个整数 left 和 right ,其中 left = right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。 链表中节点数目为 n 1 = n = 500 -500 = Node.val = 500 1 = left = right = n 你可以使用一趟扫描完成反转吗? 将链表分成3部分,即前面不需要反转的部

    2024年02月05日
    浏览(50)
  • LeetCode //C - 141. Linked List Cycle

    Given head , the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a p

    2024年02月11日
    浏览(47)
  • 【set】个人练习-Leetcode-817. Linked List Components

    题目链接:https://leetcode.cn/problems/linked-list-components/description/ 题目大意:给出一个 vectorint nums ,其中有一些数字。再给出一个链表的头指针 head ,链表内的元素各不相同。如果链表中有某一段(长度大于等于1)的元素都在 nums 中出现过,那么就算一个component,求链表中的co

    2024年02月13日
    浏览(40)
  • LeetCode //C - 328. Odd Even Linked List

    Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd, and the second node is even, and so on. Note that the relative order inside both the even and odd groups should remain as it was in the input. You must solve the proble

    2024年01月25日
    浏览(44)
  • Leetcode 1367. Linked List in Binary Tree (二叉树好题)

    Linked List in Binary Tree Medium Given a binary tree root and a linked list with head as the first node. Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False. In this context downward path means a path that starts at some node and goes downwards. Exampl

    2024年01月25日
    浏览(48)
  • LeetCode //C - 114. Flatten Binary Tree to Linked List

    Given the root of a binary tree, flatten the tree into a “linked list”: The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null. The “linked list” should be in the same order as a pre-order traversal of the binary tree.   Example 1: Input: ro

    2024年02月09日
    浏览(38)
  • LeetCode 1019. Next Greater Node In Linked List【单调栈模板】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2023年04月18日
    浏览(37)
  • LeetCode //C - 2130. Maximum Twin Sum of a Linked List

    In a linked list of size n, where n is even, the i t h i^{th} i t h node (0-indexed) of the linked list is known as the twin of the ( n − 1 − i ) t h (n-1-i)^{th} ( n − 1 − i ) t h node, if 0 = i = (n / 2) - 1. For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4. Th

    2024年01月17日
    浏览(66)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包