两两替换链表中的节点
题目形容:给定一个链表,两两替换其中相邻的节点,并返回替换后的链表。
你不能只是单纯的扭转节点外部的值,而是须要理论的进行节点替换。
示例阐明请见LeetCode官网。
起源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs/
著作权归领扣网络所有。商业转载请分割官网受权,非商业转载请注明出处。
解法一:双指针法
- 首先,如果head为空或者head没有后继节点,间接返回head;
而后用2个指针first和second别离指向第一个和第二个节点,new一个pre节点指向first,操作过程如下:
- first的next指向second的next;
- second的next指向first;
- pre的next指向second;
- 后面几步就是把first和second的地位替换;
- 而后pre指向first;
- first指向first的next;
- 而后进行下一次遍历,条件是first不为空且first的next不为空,直到这个条件不成立,则遍历完结。
- 返回后果。
public class LeetCode_024 {
public static ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newHead = head.next;
ListNode pre = new ListNode(-1), first = head, second;
pre.next = head;
while (first != null && first.next != null) {
second = first.next;
first.next = second.next;
second.next = first;
pre.next = second;
pre = first;
first = first.next;
}
return newHead;
}
public static void main(String[] args) {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
head.next.next.next.next = new ListNode(5);
ListNode result = swapPairs(head);
while (result != null) {
System.out.print(result.val + " ");
result = result.next;
}
}
}
【每日寄语】 宇宙山河浪漫,生存点滴和煦,都值得你我后退。
发表回复