reverse都是两个记录要交换的 一个记录之前的
corner:null 有一个 有两个
1 class Solution { 2 public ListNode swapPairs(ListNode head) { 3 if(head == null) return null; 4 if(head.next == null) return head; 5 ListNode node2 = head; 6 ListNode node3 = head.next; 7 if(node3.next == null) { 8 node2.next = null; 9 node3.next = node2; 10 return node3; 11 } 12 node2.next = node3.next; 13 node3.next = node2; 14 head = node3; 15 ListNode prevNode = node2; 16 while(node2.next != null && node2.next.next != null) { 17 node2 = node2.next; 18 node3 = node2.next; 19 prevNode.next = node3; 20 node2.next = node3.next; 21 node3.next = node2; 22 prevNode = node2; 23 24 } 25 return head; 26 } 27 }
原文:https://www.cnblogs.com/goPanama/p/9607290.html