Swap Nodes in Pairs Posted on 2018-08-15 Descriptionhttps://leetcode.com/problems/swap-nodes-in-pairs/description/ Solution(Sentinel node tricky)123456789101112131415161718192021222324252627282930/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode swapPairs(ListNode head) { ListNode pre = new ListNode(0); if (head == null || head.next == null) return head; ListNode first = head; ListNode second = head.next; pre.next = first; head = second; while (true) { pre.next = second; pre = first; ListNode temp = second.next; second.next = first; if (temp == null) { first.next = null; return head; } first = temp; second = first.next != null ? first.next : first; #Pay attention to the singular case } }}