链表经典算法-Golang

type ListNode struct {
    Val int
    Next *ListNode
}

//单链表反转
func reverseList(head *ListNode) *ListNode {
    var pre *ListNode
    cur := head
    for nil != cur {
        pre, cur, cur.Next = cur, cur.Next, pre
    }

    return pre
}

//检测链表是否有环
//快慢指针,如果相遇说明有环
//链表起点head到入口点的距离=slow与fast相遇点到入口的距离
func hasCycle(head *ListNode) *ListNode {
    fast, slow := head, head
    for nil != fast && nil != fast.Next {
        slow, fast = slow.Next, fast.Next.Next
        if slow == fast {
            break
        }
    }

    if nil == slow || nil == fast.Next {
        return nil
    }

    slow = head
    for slow != fast {
        slow, fast = slow.Next, fast.Next
    }

    return slow
}

//合并两个有序链表
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
    //如果其中一个为空则返回另一
    if nil == l1 {
        return l2
    }

    if nil == l2 {
        return l1
    }

    var head, cur *ListNode
    if l1.Val < l2.Val {
        head, cur, l1 = l1, l1, l1.Next
    } else {
        head, cur, l2 = l2, l2, l2.Next
    }

    //循环,直到其中一个链表遍历完
    for nil != l1 && nil != l2 {
        if l1.Val < l2.Val {
            cur.Next = l1
            l1 = l1.Next
        } else {
            cur.Next = l2
            l2 = l2.Next
        }
        cur = cur.Next
    }
    if nil != l1 {
        cur.Next = l1
    }

    if nil != l2 {
        cur.Next = l2
    }

    return head
}

//删除链表倒数第N个节点
func removeNthFromEnd(head *ListNode, n int) *ListNode {
    result := &ListNode{}
    result.Next = head

    var pre *ListNode
    cur := result

    i := 1
    for nil != head {
        if i >= n {
            pre = cur
            cur = cur.Next
        }
        head = head.Next
        i++
    }

    pre.Next = pre.Next.Next

    return result.Next
}

//链表中间节点
//快慢指针,快指针走到头,慢必定在中间节点
func middleNode(head *ListNode) *ListNode {
    fast, slow := head, head
    for nil != fast && nil != fast.Next {
        fast = fast.Next.Next
        slow = slow.Next
    }

    return slow
}

 

你可能感兴趣的:(Golang)