一、原题要求:
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
示例:
给定1->2->3->4
, 你应该返回2->1->4->3
说明:
- 你的算法只能使用常数的额外空间。
- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
解题思路:
使用一个新的节点pre来辅助操作,对要进行交换的链表,每两个的位置进行交换,
并且把交换后的结点接到pre的链表上,直到所有的结点都处理完。
因为比较简单就直接上代码了
二、代码实现:
/**
* @ClassName: Main
* @Description: TODO
* @Author: Mr.Ye
* @Data: 2018-11-17 19:59
* @Version: 1.0
**/
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode old = new ListNode(0);
old.next = head;
ListNode pre = old;
while (pre.next != null && pre.next.next != null){
ListNode node1 = pre.next;
ListNode node2 = node1.next;
ListNode late = node2.next;
pre.next = node2;
node2.next = node1;
node1.next = late;
pre = node1;
}
return old.next;
}
}
public class Main {
public static void main(String[] args) {
ListNode n1 = new ListNode(1);
ListNode n2 = new ListNode(4);
ListNode n3 = new ListNode(3);
ListNode n4 = new ListNode(2);
ListNode n5 = new ListNode(5);
ListNode n6 = new ListNode(2);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
n5.next = n6;
System.out.println("初始链表:");
print(n1);
Solution solution = new Solution();
System.out.println("最终链表:");
print(solution.swapPairs(n1));
}
public static void print(ListNode head) {
for (ListNode temp = head; temp != null; temp = temp.next) {
System.out.print(temp.val + "->");
}
System.out.println("null");
}
}