给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。
输入: head = [1,2,3,4,5], left = 2, right = 4 输出: [1,4,3,2,5]
输入: head = [5], left = 1, right = 1 输出: [5]
// Definition for singly-linked list. // #[derive(PartialEq, Eq, Clone, Debug)] // pub struct ListNode { // pub val: i32, // pub next: Option> // } // // impl ListNode { // #[inline] // fn new(val: i32) -> Self { // ListNode { // next: None, // val // } // } // } impl Solution { pub fn reverse_between(head: Option >, left: i32, right: i32) -> Option > { // 来个哑节点方便处理头节点在反转范围内的情况 let mut dummy = Option::Some(Box::new(ListNode::new(0))); dummy.as_mut().unwrap().next = head; // 前面不需要反转部分的尾 let mut pre = dummy.as_mut().unwrap(); // 跳过前面不需要翻转的部分 for _ in 0..left - 1 { pre = pre.next.as_mut().unwrap(); } // 中间需要反转的部分(同时打断前面不需要反转部分和中间需要反转部分的链接) let mut cur = pre.next.take(); // 进行反转 for _ in 0..right - left { let mut next = cur.as_mut().unwrap().next.take(); cur.as_mut().unwrap().next = next.as_mut().unwrap().next.take(); next.as_mut().unwrap().next = pre.next.take(); pre.next = next; } // 重新接上 while pre.next.is_some() { pre = pre.next.as_mut().unwrap(); } pre.next = cur; return dummy.unwrap().next; } }
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ func reverseBetween(head *ListNode, left int, right int) *ListNode { // 来个哑节点方便处理头节点在反转范围内的情况 dummy := &ListNode{Val: 0, Next: head} pre := dummy for i := 0; i < left-1; i++ { pre = pre.Next } cur := pre.Next for i := 0; i < right-left; i++ { next := cur.Next cur.Next = next.Next next.Next = pre.Next pre.Next = next } return dummy.Next }
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* reverseBetween(ListNode* head, int left, int right) { // 来个哑节点方便处理头节点在反转范围内的情况 ListNode *dummy = new ListNode(0); dummy->next = head; ListNode *pre = dummy; for (int i = 0; i < left - 1; ++i) { pre = pre->next; } ListNode *cur = pre->next; ListNode *next; for (int i = 0; i < right - left; ++i) { next = cur->next; cur->next = next->next; next->next = pre->next; pre->next = next; } return dummy->next; } };
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]: # 来个哑节点方便处理头节点在反转范围内的情况 dummy = ListNode(0) dummy.next = head pre = dummy for _ in range(left - 1): pre = pre.next cur = pre.next for _ in range(right - left): next = cur.next cur.next = next.next next.next = pre.next pre.next = next return dummy.next
/** * 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 reverseBetween(ListNode head, int left, int right) { // 来个哑节点方便处理头节点在反转范围内的情况 ListNode dummy = new ListNode(0); dummy.next = head; ListNode pre = dummy; for (int i = 0; i < left - 1; i++) { pre = pre.next; } ListNode cur = pre.next; ListNode next; for (int i = 0; i < right - left; i++) { next = cur.next; cur.next = next.next; next.next = pre.next; pre.next = next; } return dummy.next; } }
非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】三连走一波~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~
上一篇:Http协议与Tomcat