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

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 struct ListNode* swapPairs(struct ListNode* head)
 9 {
10         struct ListNode *p=head,*q=head;
11
12     int LiskLen=0;
13
14     while(q!=NULL)
15     {
16         LiskLen++;
17         q=q->next;
18     }
19     if(LiskLen%2==0)
20     {
21         while(p!=NULL)
22         {
23             int temp;
24             temp=p->val;
25             p->val=p->next->val;
26             p->next->val=temp;
27
28             p=p->next->next;
29         }
30     }
31     else
32     {
33         while(p->next!=NULL)
34         {
35             int temp;
36             temp=p->val;
37             p->val=p->next->val;
38             p->next->val=temp;
39
40             p=p->next->next;
41         }
42     }
43
44     return head;
45 }
时间: 2024-07-30 03:25:37

LeeCode-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 t

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

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

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

63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

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

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Total Submissions: 39302 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the

每日算法之二十二: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 链表成对交换节点

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 链表指针的应用

题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pairs * 题目:输入一个链表,要求将链表每相邻的两个节点交换位置后输出 * 思路:遍历一遍就可以,时间复杂度O(n) * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * Li

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

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