《leetcode-go》链表内指定区间反转

这篇博客探讨了如何在O(1)空间复杂度和O(n)时间复杂度下,对链表的指定区间[m, n]进行反转。通过创建辅助节点,找到m和n的位置,然后进行反转操作,最后将反转部分正确地重新连接到原始链表中。示例展示了如何处理这种问题,并给出了相应的解决方案。

将一个链表m 位置到n 位置之间的区间反转,要求时间复杂度 ,空间复杂度 
例如:
给出的链表为 ,
返回 .
注意:
给出的 , 满足以下条件:

思路:找到m的位置,开始反转链表,跟普通反转链表不同的是,他需要记录m的前一个位置,方便把m-n这部分的头拼接上,还需要记录n的后一个位置,把这个链表的尾接上,防止head发生变化,新建一个新的head。

package main
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
  * 
  * @param head ListNode类 
  * @param m int整型 
  * @param n int整型 
  * @return ListNode类
*/
func reverseBetween( head *ListNode ,  m int ,  n int ) *ListNode {
    // write code here
    if m == n {
		return head
	}
	first := new(ListNode)
	first.Next = head
	node := head
	var start *ListNode
	var stop *ListNode
	var last *ListNode
	var pre = first
	for node != nil {
		m--
		n--
		if m == 0 {
			last = pre
			start = node
		}
		if n == 0 {
			stop = node.Next
			node.Next = nil
		}
		pre = node
		node = node.Next
	}
	if last != nil {
		last.Next = nil
	}
	node = new(ListNode)
	node.Next = start
	pre = nil
	for start != nil {
		tmp := start.Next
		start.Next = pre
		pre = start
		start = tmp
	}
	if last != nil {
		last.Next = pre
	}
	node.Next.Next = stop
	return first.Next
}

### 链表反转算法的实现方法 链表反转是一个经典的算法问题,在不同的编程语言中有多种实现方式。以下是几种常见的方式及其具体实现。 #### 使用迭代的方法(以C为例) 在C语言中,可以使用三个指针变量`pre`、`cur`和`temp`来完成链表反转操作。初始时,`pre`设为`NULL`,表示新的链表头部;`cur`指向原始链表的头节点。每次循环中,先保存`cur->next`到临时变量`temp`,然后修改`cur->next`使其指向`pre`,最后更新`pre`和`cur`的位置以便处理下一个节点[^4]。 ```c struct ListNode* reverseList(struct ListNode* head) { struct ListNode* pre = NULL; struct ListNode* cur = head; while (cur != NULL) { struct ListNode* temp = cur->next; cur->next = pre; pre = cur; cur = temp; } return pre; } ``` #### 使用递归的方法(以Python为例) 递归方法的核心思想是从链表的尾部开始逐步构建反转后的链表。对于每个节点,将其后续部分视为已经反转好的子链表,然后调整当前节点与其后续节点之间的关系。 ```python def reverse_list(head): if not head or not head.next: return head reversed_head = reverse_list(head.next) head.next.next = head head.next = None return reversed_head ``` #### C#中的链表反转实现 在C#中,同样可以通过遍历链表并逐个更改节点间的链接方向来实现链表反转。这种方法类似于C语言中的迭代法[^3]。 ```csharp public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; } } public static ListNode ReverseList(ListNode head) { ListNode prev = null; ListNode current = head; while(current != null){ ListNode nextTemp = current.next; current.next = prev; prev = current; current = nextTemp; } return prev; } ``` #### 双向链表反转 对于双向链表而言,除了需要交换前后两个方向上的指针外,还需要特别注意首尾节点的变化以及可能存在的边界条件处理[^5]。 ```java public void reverse() { Node<T> temp = null; Node<T> current = head; while (current != null) { temp = current.prev; current.prev = current.next; current.next = temp; current = current.prev; } if(temp != null ){ head = temp.prev; } } ``` 以上就是关于单向与双向链表反转的一些基本思路及其实现代码示例。每种语言都有自己的特点,因此具体的编码风格可能会有所差异,但核心逻辑保持一致。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值