Swap Nodes in Pairs -- leetcode

原创 2016年08月30日 16:33:07

题目描述:
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.

大意是:成对的交换链表的结点,1和2交换;3和4交换,5和6交换等。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == NULL || head->next == NULL) return head;
        ListNode *newList = new ListNode(0);
        newList->next = head;
        head = newList;
       while(head->next != NULL && head->next->next != NULL){
           ListNode *p1 = head -> next;//取出第一个节点结点 
           ListNode *p2 = head -> next -> next;//取出第二个节点结点
           // head->p1->p2->...  ---->> head->p2->p1->...
           head -> next = p2;
           p1 -> next = p2 -> next;
           p2 -> next = p1;
           //move head to p1
           head = p1;
       }
       return newList->next;
    }
};
版权声明:本文为博主原创文章,欢迎转载,请注明出处。 https://blog.csdn.net/Alpaca12/article/details/52370578

Swap Nodes in Pairs -- LeetCode

原题链接: http://oj.leetcode.com/problems/swap-nodes-in-pairs/  这道题属于链表操作的题目,思路比较清晰,就是每次跳两个节点,后一个接到前面,前一...
  • linhuanmars
  • linhuanmars
  • 2014-02-26 01:37:59
  • 12333

LeetCode 24 Swap Nodes in Pairs(交换序列中的结点)(Linked List)

翻译给定一个链表,调换每两个相邻节点,并返回其头部。例如, 给定 1->2->3->4, 你应该返回的链表是 2->1->4->3。你的算法必须使用唯一不变的空间。你也不能修改列表中的值,只有节点本身...
  • NoMasp
  • NoMasp
  • 2015-11-12 18:43:57
  • 4541

[LeetCode] 024. Swap Nodes in Pairs (Medium) (C++/Python)

[LeetCode] 024. Swap Nodes in Pairs (Medium) (C++/Python)
  • hcbbt
  • hcbbt
  • 2015-03-05 14:39:27
  • 1682

LeetCode24. Swap Nodes in Pairs简单到一次性通过

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-...
  • booirror
  • booirror
  • 2016-02-02 21:01:22
  • 1630

LeetCode 24 Swap Nodes in Pairs (C,C++,Java,Python)

Problem: Given a linked list, swap every two adjacent nodes and return its head. For example...
  • runningtortoises
  • runningtortoises
  • 2015-05-11 16:37:22
  • 1558

(Java)LeetCode-24. Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-...
  • u012848330
  • u012848330
  • 2016-06-02 16:53:15
  • 535

24. Swap Nodes in Pairs [easy] (Python)

题目链接https://leetcode.com/problems/swap-nodes-in-pairs/题目原文 Given a linked list, swap every two adj...
  • coder_orz
  • coder_orz
  • 2016-05-29 15:09:02
  • 1071

24. Swap Nodes in Pairs -leetcode-java

今天一下午被这道题困惑好久,也是够笨呀。 Given a linked list, swap every two adjacent nodes and return its head. ...
  • hahawhyha
  • hahawhyha
  • 2016-05-26 18:54:52
  • 680

【LeetCode-24】Swap Nodes in Pairs(C++)

交换链表中每两个相邻的节点 不能改变节点的值 智能改变节点。 递归法: class Solution { public: ListNode* swapPairs(ListNode* head)...
  • liujiayu1015
  • liujiayu1015
  • 2016-08-08 16:57:34
  • 140

[C++]LeetCode: 109 Swap Nodes in Pairs (交换相邻节点位置)

题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->...
  • cinderella_niu
  • cinderella_niu
  • 2015-01-18 17:41:36
  • 591
收藏助手
不良信息举报
您举报文章:Swap Nodes in Pairs -- leetcode
举报原因:
原因补充:

(最多只允许输入30个字)