swap_node_in_pairs

  • 2022-12-14
  • 浏览 (453)

swap_node_in_pairs.go 源码

package main

//两两交换链表中的节点

type ListNode struct {
	Val  int
	Next *ListNode
}

// 递归
func swapPairs(head *ListNode) *ListNode {
	if head == nil || head.Next == nil {
		return head
	}

	one, two, three := head, head.Next, head.Next.Next
	two.Next = one
	one.Next = swapPairs(three)
	return two
}

func swapPairs1(head *ListNode) *ListNode {
	if head == nil || head.Next == nil {
		return head
	}

	node := &ListNode{-1, nil}
	node.Next = head
	p := node
	for p.Next != nil && p.Next.Next != nil {
		front, back := p.Next, p.Next.Next
		p.Next = back
		front.Next = back.Next
		back.Next = front
		p = p.Next.Next
	}
	return node.Next
}

你可能感兴趣的文章

add_two_numbers

add_two_numbers_ii

intersection_linked_lists

0  赞