image.png

    1. class Solution {
    2. public static ListNode deleteDuplicates(ListNode head) {
    3. //边界情况:空链表、单节点不用反转
    4. if(head == null || head.next == null)
    5. return head;
    6. ListNode thisNode = head;
    7. ListNode nextNode = head.next;
    8. ListNode tempNode;
    9. while(nextNode.next != null){
    10. //反转nextNode的指向
    11. tempNode = nextNode.next;
    12. nextNode.next = thisNode;
    13. //将thisNode和nextNode都各自推进一步
    14. thisNode = nextNode;
    15. nextNode = tempNode;
    16. }
    17. //循环结束后,最后一个节点即nextNode还未改变指向
    18. nextNode.next = thisNode;
    19. //head节点还未处理
    20. head.next = null;
    21. return nextNode;
    22. }
    23. }