LeetCode24:
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
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) {if (head == null) {return head;}ListNode pre = head;ListNode cur = head.next;ListNode temp = null;if (cur != null){temp = cur;}else {temp = head;}while (cur != null) {pre.next = cur.next;cur.next = pre;pre = pre.next;cur = cur.next;cur = cur.next;if (cur == null) {break;}cur = cur.next;}return temp;}}
