[leedcode 24] 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.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode swapPairs(ListNode head) {
        //本题是两个节点进行反转,主要的思想是画图,确定每次循环之前,各个指针的位置
        //注意:1.声明一个新的头结点(很多题都是这种方法),只需在返回时注意返回新头结点的next即可
        //2.注意当链表为空,以及只有一个节点时的处理方法
        if(head==null||head.next==null)
            return head;
        ListNode newHead=new ListNode(-1);
        ListNode last=newHead;
        ListNode s=head;
        ListNode t=head;
        while(t!=null&&t.next!=null){
            t=s.next.next;
            last.next=s.next;
            last.next.next=s;
            s.next=t;
            last=s;
            s=t;
        }
        return newHead.next;

    }
}
时间: 2024-11-12 19:27:25

[leedcode 24] Swap Nodes in Pairs的相关文章

leetCode 24. Swap Nodes in Pairs 链表

24. 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

24. Swap Nodes in Pairs(js)

24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list's nodes, only nodes itself may be changed. Example: Given 1->2->3->4, you should return the list as 2->1-

[Leetcode][Python]24: Swap Nodes in Pairs

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 24: Swap Nodes in Pairshttps://oj.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 shoul

leetCode 24. 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】24. Swap Nodes in Pairs

题目描述: Given a linked list, swap every two adjacent nodes and return its head. 解题分析: 解题思路很简单,就是先两两扫描,然后调换顺序即可.这道题的难点在于每个节点的next域指向问题,所以解这样的题最好可以在纸上写一下交换的步骤就不难实现 具体代码: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val

24. Swap Nodes in Pairs Java solutions

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

24. Swap Nodes in Pairs

1 class Solution { 2 public: 3 ListNode* swapPairs(ListNode* head) { 4 ListNode *t1,*t2,*t3,*t4; 5 if(head==NULL||head->next==NULL) 6 return head; 7 t4->next=t2->next; 8 t3=t2->next->next; 9 t2->next->next=t2; 10 t2->next=t3; 11 wh

LeetCode #24 Swap Nodes in Pairs (M)

[Problem] 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

24.Swap Nodes in Pairs (List; Two-Pointers)

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