funcremoveNthFromEnd(head *ListNode, n int) *ListNode { t := &ListNode{0, head} p, c := t, t for n > 0 && p != nil { p = p.Next n-- } if p == nil { returnnil } // 这里需要注意 不能多走 for p.Next != nil { p = p.Next c = c.Next } c.Next = c.Next.Next return t.Next }
Linked List Cycle
No.141
Given head, the head of a linked list, determine if the linked list has a cycle in it.
1 2 3 4 5 6 7 8 9 10 11
funchasCycle(head *ListNode)bool { t1 := head for head != nil && head.Next != nil { head = head.Next.Next t1 = t1.Next if head == t1 { returntrue } } returnfalse }
Intersection of Two Linked Lists
No.160
Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.