package main import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * * @param head ListNode类 * @param n int整型 * @return ListNode类 */ func removeNthFromEnd( head *ListNode , n int ) *ListNode { // write code here var pWork *ListNode = head var pWorkPre *ListNode = nil var pCountLen *ListNode = pWork var lstLen int = 0 for pCountLen != nil { // 确认链表元素个数 lstLen++ pCountLen = pCountLen.Next } for lstLen - n + 1 > 1 { //元素的倒数第N个,就是正着数的第lstLen-n+1个 lstLen就是元素总数 pWorkPre = pWork pWork = pWork.Next lstLen-- } if pWorkPre == nil { //如果要删除的节点为正数第一个,则直接遍历一次,返回指向第二个节点的指针 pWork = pWork.Next return pWork } pWorkPre.Next = pWork.Next return head }