Remove Nth Node From End of List
No.19
Given the head of a linked list, remove the nth node from the end of the list and return its head.
解题思路:需要移除倒数第N个节点,只需要找到n-1个节点x,将x.next指向x.next.next即可
1 | func removeNthFromEnd(head *ListNode, n int) *ListNode { |
Linked List Cycle
No.141
Given head, the head of a linked list, determine if the linked list has a cycle in it.
1 | func hasCycle(head *ListNode) bool { |
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.
还有另外两种解法1.找出长度差,然后让长链表从到终点与短链表长度一致处出发,循环判断 2.把链表连接起来,寻找环的起点即可
1 | func getIntersectionNode(headA, headB *ListNode) *ListNode { |