美文网首页
Tourist with Data Structure Seco

Tourist with Data Structure Seco

作者: Jiawei_84a5 | 来源:发表于2019-05-24 13:53 被阅读0次

链表

读题要仔细,只看题干,容易死的很惨。

设计链表

type node struct {
    val int
    next *node
}

type MyLinkedList struct {
    size int
    head , tail *node
}


/** Initialize your data structure here. */
func Constructor() MyLinkedList {
    t := &node{}
    h := &node{next: t}
    
    return MyLinkedList{
        head: h,
        tail: t,
    }
}


/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
func (this *MyLinkedList) Get(index int) int {
    if index < 0 || this.size <= index {
        return -1
    }
    
    i , temp := 0 , this.head.next
    
    for i < index {
        i++
        temp = temp.next
    }
    
    return temp.val
    
}


/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
func (this *MyLinkedList) AddAtHead(val int)  {
    temp := &node{val: val}
    
    temp.next = this.head.next
    this.head.next = temp
    this.size++
}


/** Append a node of value val to the last element of the linked list. */
func (this *MyLinkedList) AddAtTail(val int)  {
    this.tail.val = val
    temp := &node{}
    this.tail.next = temp
    this.tail = temp
    this.size++
}


/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
func (this *MyLinkedList) AddAtIndex(index int, val int)  {
    if this.size < index {
        return 
   } else if index <= 0 { //this part was check the discussion part , if index < 0 we will add at head.
        this.AddAtHead(val)
        return
    } else if index == this.size {
        this.AddAtTail(val)
        return
    }
    
    i, temp := -1, this.head
    
    for i+1 < index {
        i++
        temp = temp.next
    }
    
    nd := &node{val: val}
    nd.next = temp.next
    temp.next = nd
    
    this.size++
}


/** Delete the index-th node in the linked list, if the index is valid. */
func (this *MyLinkedList) DeleteAtIndex(index int)  {
    if (index <0 || this.size <= index) {
        return
    }
    
    i, temp := -1, this.head

    for i + 1 < index {
        i++
        temp = temp.next
    }
    
    temp.next = temp.next.next
    
    this.size--
}


/**
 * Your MyLinkedList object will be instantiated and called as such:
 * obj := Constructor();
 * param_1 := obj.Get(index);
 * obj.AddAtHead(val);
 * obj.AddAtTail(val);
 * obj.AddAtIndex(index,val);
 * obj.DeleteAtIndex(index);
 */

环形链表

一般环形链表使用快慢指针方式去做,快慢指针算法。
参看解释

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func hasCycle(head *ListNode) bool {
    if head == nil {
        return false
    }
    
    fast := head.Next
    slow := head
    
    for slow != nil && fast != nil && fast.Next != nil {
        slow = slow.Next
        fast = fast.Next.Next
        
        if fast == slow {
            return true
        }
    }
    
    return false
}

环形链表II

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func detectCycle(head *ListNode) *ListNode {
    if head == nil || head.Next == nil {
        return nil
    }
    
    slow , fast := head.Next , head.Next.Next
    
    //if has cycle finally slow == fast. then loop break.
    //if don't have cycle finally fast.Next == nil . then loop break.
    for fast != nil && fast.Next != nil && slow != fast {
        slow , fast = slow.Next , fast.Next.Next
    }
    
    //no cycle.
    if slow != fast {
        return nil
    }
    //if has cycle , now the slow's position to the entrance will equal to the head to the entrance.
    for slow != head {
        slow, head = slow.Next, head.Next
    }
    
    return slow
}

相交链表

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func getIntersectionNode(headA, headB *ListNode) *ListNode {
    //when the node catch up , the link list length is the same
    
    a , b := headA , headB
    
    lengthA , lengthB := 0 , 0
    
    for a != nil {
        a = a.Next
        lengthA++
    }
    
    for b != nil {
        b = b.Next
        lengthB++
    }
    
    a , b = headA , headB
    
    for lengthA < lengthB {
        b = b.Next
        lengthB--
    }
    
    for lengthA > lengthB {
        a = a.Next
        lengthA--
    }
    
    for a != b {
        a = a.Next
        b = b.Next
    }
    
    return a
    
}

删除链表的倒数第N个节点

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func removeNthFromEnd(head *ListNode, n int) *ListNode {
    //keep fast and slow in n distance , when fast get the end of the list , slow is in the position
    
    fast := head
    slow := head
    
    for i := 0 ;i < n ; i++ {
        if fast.Next != nil {
            fast = fast.Next
        } else {
            //for the first value.
            return head.Next
        }
    }
    
    for fast.Next != nil {
        fast = fast.Next
        slow = slow.Next
    }
    
    slow.Next = slow.Next.Next
    
    return head
}

反转链表

参考

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func reverseList(head *ListNode) *ListNode {
    var newHead ListNode
    
    for head != nil { 
        newHead.Next, head.Next, head = head, newHead.Next, head.Next
    }
    
    return newHead.Next
}

移除链表元素

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func removeElements(head *ListNode, val int) *ListNode {
    if head == nil {
        return head
    }
    
    var orig ListNode
    orig.Next = head
    temp := &orig
    
    for head != nil {
        if head.Val == val {
            temp.Next , head = head.Next , head.Next
        } else {
            temp , head = head , head.Next
        }
    }
    
    
    return orig.Next
}

回文链表

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func isPalindrome(head *ListNode) bool {
    if head == nil || head.Next == nil {
        return true
    }
    
    fast, slow, tmp := head, head, head
    
    var l ListNode
    
    for fast != nil && fast.Next !=  nil {
        fast = fast.Next.Next
        
        tmp = slow.Next
        slow.Next = l.Next
        l.Next = slow
        slow = tmp
    }
    
    if fast != nil {
        slow = slow.Next
    }
    
    tmp = l.Next
    for slow != nil && tmp != nil && slow.Val == tmp.Val {
        slow, tmp = slow.Next, tmp.Next
    }
    
    return slow == nil
    
    
}

合并两个有序链表

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
    if l1 == nil {
        return l2
    }
    if l2 == nil {
        return l1
    }
    if l1.Val < l2.Val {
        l1.Next = mergeTwoLists(l1.Next, l2)
        return l1
    } else {
        l2.Next = mergeTwoLists(l1, l2.Next)
        return l2
    }
    
}

旋转链表

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func rotateRight(head *ListNode, k int) *ListNode {
        if head == nil || head.Next == nil || k == 0 {
        return head
    }
    len := 1
    tail := head
    for tail.Next != nil {
        tail = tail.Next
        len++
    }
    tail.Next = head //become a cycle
    k = k % len      //we made this a cycle , so we don't need this to round more than 1 time
    for i := 0; i < len-k; i++ {
        tail = tail.Next
    }
    head = tail.Next
    tail.Next = nil
    return head
}

相关文章

  • Tourist with Data Structure Seco

    链表 读题要仔细,只看题干,容易死的很惨。 设计链表 环形链表 一般环形链表使用快慢指针方式去做,快慢指针算法。参...

  • Tourist with Data Structure Firs

    数组和字符串 寻找数组的中心索引. 本来使用暴力破解方法, 但是实在是,看不下去了,想了半个小时终于想出来一个,我...

  • Tourist with Data Structure Thir

    探索哈希表 概念 哈希集合:哈希集合是集合数据结构的实现之一,用于存储非重复值。哈希映射 :哈希映射是映射数据结构...

  • Tourist with Data Structure Fift

    设计循环列表 岛屿的数量 最小栈 有效的括号 每日温度 逆波兰表达式求值 克隆图 目标和 用栈实现队列 用栈实现队...

  • Tourist with Data Structure Fout

    递归 反转字符串 这个以前实现过, 这次试用递归的方式实现。 两两交换链表中的节点 不知道如何试用递归来完成。不过...

  • Data Structure

    stack heap 堆的基本性质:①堆中的某一个节点总是不小于或不大于其父节点的值。②堆总是一棵完全二叉树比较经...

  • Data Structure

    ds_using_list.py ds_using_tuple.py ds_using_dict.py ds_se...

  • Data structure

  • SICP-chapter2 Data Structure

    Introduction to Data Structure What is Meant by Data? The...

  • [Math] Persistent data structure

    In computing, a persistent data structure is a data struc...

网友评论

      本文标题:Tourist with Data Structure Seco

      本文链接:https://www.haomeiwen.com/subject/slbvzqtx.html