24. 兩兩交換鏈表中的節(jié)點
思想:重點在于如何交換兩個節(jié)點,順序如圖所示:

交換兩個節(jié)點.png
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(-1,head);
ListNode prev = dummy;
while(prev.next != null && prev.next.next != null){
ListNode temp = head.next.next;
prev.next = head.next;
head.next.next = head;
head.next = temp;
prev = head;
head = head.next;
}
return dummy.next;
}
}
19.刪除鏈表的倒數(shù)第N個節(jié)點
思路:雙指針的典型應(yīng)用。