LeetCode——Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

For example,

Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

原题链接:https://oj.leetcode.com/problems/swap-nodes-in-pairs/

题目:给定一个链表,交换两个邻接节点并返回。

你的算法只能使用常数空间。不能修改表中的值,只有节点自身可以改变。

思路:首先,直接交换两个节点的。可以通过,但是不符合要求。

	public ListNode swapPairs(ListNode head) {
		if (head == null || head.next == null)
			return head;
		ListNode back = head;
		while (back != null) {
			if (back.next != null) {
				int val = back.val;
				back.val = back.next.val;
				back.next.val = val;
				back = back.next.next;
			} else
				break;
		}
		return head;
	}

第2种方法:先设置一个虚拟的头节点,防止为空。再交换节点的引用。

	public ListNode swapPairs(ListNode head) {
		ListNode dummy = new ListNode(-1);
		dummy.next = head;
		ListNode pre = dummy;
		while (head != null && head.next != null) {
			ListNode tmp = head.next.next;
			pre.next = head.next;
			head.next.next = head;
			head.next = tmp;
			pre = head;
			head = tmp;
		}
		return dummy.next;
	}

LeetCode——Swap Nodes in Pairs,布布扣,bubuko.com

时间: 2024-10-12 06:55:47

LeetCode——Swap Nodes in Pairs的相关文章

Leetcode:Swap Nodes in Pairs 链表成对交换节点

Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the va

LeetCode: Swap Nodes in Pairs [023]

1. create a new accout, create orginazation, create repo 2. install git in your local pc Note: you can create ssh key to avoid username/password input for github operation https://help.github.com/articles/generating-ssh-keys https://help.github.com/a

Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, on

leetcode | Swap Nodes in Pairs in a linklist

Swap Nodes in Pairs : https://leetcode.com/problems/swap-nodes-in-pairs/ Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm shou

LeetCode: Swap Nodes in Pairs 解题报告

Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the val

[Leetcode]Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, onl

[LeetCode]Swap Nodes in Pairs 成对交换

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, on

[LeetCode] Swap Nodes in Pairs 成对交换节点

Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, onl

leetcode Swap Nodes in Pairs python

# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ if hea