链表算法题

package code

import (
	"fmt"
)


/*
   链表的结构
*/
type ListNode struct {
	Val int
	Len int
	Next *ListNode
}


/*
   输出链表
*/
func PrintLink(p *ListNode) {
	for p != nil {
		fmt.Printf("%d->",p.Val)
		p = p.Next
	}
	fmt.Printf("nil \n")
}

/**
数组转链表构造链表
 */
func ArrToLinkList(head *ListNode,arr []int){
	cur := head
	for i := 0; i < len(arr); i++ {
		cur.Next = &ListNode{}
		cur.Next.Val = arr[i]
		cur = cur.Next
	}
}

/**
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Return the linked list sorted as well.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5

Example 2:
Input: 1->1->1->2->3
Output: 2->3
*/
func DeleteDuplicates(head *ListNode) *ListNode {
	tmp:=new(ListNode)
	tmp.Next = head
	fast:=head
	slow:=head
	res:=tmp
	for slow!=nil {
		if fast!=nil && slow.Val==fast.Val{
			fast = fast.Next
		}else if slow.Next!=fast {
			res.Next = fast
			slow = fast
		}else if slow.Next==fast{
			res  = res.Next
			slow = slow.Next
			fast = slow
		}
	}
	return res.Next
}

/**
判断链表有环
造一堆链表、将最后一个的指针指向前面任意一个
node1 := new(code.ListNode)~node5 := new(code.ListNode)~m
node5 := new(code.ListNode)
node1.Val = 1	node2.Val = 2	node3.Val = 3	node4.Val = 4	node5.Val = 5
node1.Next = node2	node2.Next = node3	node3.Next = node4	node4.Next = node5	node5.Next = nil
fmt.Println(code.HasCycle(node1))
 */
func HasCycle(head *ListNode) bool {
	slow := head
	fast := head
	for fast != nil && fast.Next != nil {
		slow = slow.Next
		fast = fast.Next.Next
		if fast == slow {
			return true
		}
	}
	return false
}


func ReverseNode(head *ListNode) *ListNode {
//  先声明两个变量
//  前一个节点
	var preNode *ListNode
	preNode = nil

//  后一个节点
	Next := new(ListNode)
	Next = nil

	for head != nil {
		//  保存头节点的下一个节点,
		Next = head.Next
		//  将头节点指向前一个节点
		head.Next = preNode
		//  更新前一个节点
		preNode = head
		//  更新头节点
		head = Next
	}
	return preNode
}

/**
* 链表翻转
**/
func ReverseList(head *ListNode) *ListNode {
	cur := head
	var pre *ListNode
	for cur != nil {
		pre, cur, cur.Next = cur, cur.Next, pre
	}
	return pre
}



/**
* 奇偶链表
**/
func OddEvenList(head *ListNode) *ListNode {
	if head == nil || head.Next== nil {
		return head
	}
	base := head
	cur := head.Next
	
	for cur!=nil && cur.Next!=nil {
		tmp:=base.Next
		base.Next=cur.Next
		cur.Next=cur.Next.Next
		base.Next.Next=tmp;		
		base=base.Next
		cur=cur.Next
	}
	return head
}


/**
Merge two sorted linked lists and return it as a new list.
The new list should be made by splicing together the nodes of the first two lists.
Example:
	Input: 1->2->4, 1->3->4
	Output: 1->1->2->3->4->4
解题思路
题意是用一个新链表来合并两个已排序的链表,
那我们只需要从头开始比较已排序的两个链表,
新链表指针每次指向值小的节点,依次比较下去,最后,当其中一个链表到达了末尾,
我们只需要把新链表指针指向另一个没有到末尾的链表此时的指针即可。
*/
func MergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
	head := &ListNode{}
	tmp := head
	for l1 != nil && l2 != nil {
		if l1.Val < l2.Val {
			tmp.Next = l1
			l1 = l1.Next
		} else {
			tmp.Next = l2
			l2 = l2.Next
		}
		tmp = tmp.Next
	}
	if l1 != nil {
		tmp.Next = l1
	} else {
		tmp.Next = l2
	}

	return head.Next
}

/**
递归合并有序链表
 */
func MergeTwoLists2(l1 *ListNode, l2 *ListNode) *ListNode {
	//如果有一条链是nil,直接返回另外一条链
	if l1 == nil {
		return l2
	}
	if l2 == nil {
		return l1
	}
	// 定义一个结果节点
	var res *ListNode
	// 当l1节点的值大于l2节点的值,那么res指向l2的节点,从l2开始遍历,反之从l1开始
	if l1.Val >= l2.Val {
		res = l2
		res.Next = MergeTwoLists2(l1, l2.Next)
	} else {
		res = l1
		res.Next = MergeTwoLists2(l1.Next, l2)
	}
	return res
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值