206. 反转链表

总结 反转链表需要做两件事情
1 把链表尾结点一直向上归,作为头结点。
2 两个节点之间的关系next指向需要倒置过来 4->5 需要变成4<-5 ,还需要把4->去掉

gif5新文件.gif

图片来源https://leetcode-cn.com/problems/reverse-linked-list/solution/dong-hua-yan-shi-206-fan-zhuan-lian-biao-by-user74/
我把作者的PPT图片做成了GIF格式

  1. func revertNode(head *ListNode)*ListNode{
  2. if head==nil||head.Next==nil {
  3. return head
  4. }
  5. n := revertNode(head.Next)
  6. next.Next = head
  7. head.Next = nil
  8. return n
  9. }