leetcode 24

链表操作的,要注意标记头结点和边界问题。

代码如下:

 1 ListNode *swapPairs(ListNode *head) {
 2         if(head==NULL||head->next==NULL)
 3             return head;
 4         ListNode* p=head;
 5         ListNode* q=head->next;
 6         ListNode* x=head->next->next;
 7         ListNode* t=NULL;
 8         head=q;
 9         while(p!=NULL&&q!=NULL){
10             q->next=p;
11             p->next=NULL;
12             if(t!=NULL)
13                 t->next=q;
14             t=p;
15
16             p=x;
17             if(x!=NULL)
18                 q=x->next;
19             if(q!=NULL)
20                 x=q->next;
21
22         }
23         if(p!=NULL)
24             t->next=p;
25         return head;
26 }  

更加巧妙的解法,直接对链表中节点的val进行交换,不用操作链表;

代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* swapPairs(ListNode* head) {
12         int n;
13         ListNode* node = head;
14         while (head != NULL && head->next != NULL) {
15             n = head->val;
16             head->val = head->next->val;
17             head->next->val = n;
18             head = head->next->next;
19         }
20         return node;
21     }
22 };
时间: 2024-10-10 21:35:06

leetcode 24的相关文章

LeetCode(24) - Swap Nodes in Pairs

题目要求很简单,就是把list里面的node两两互换. 当做比较复杂的LinkedList的题目的时候,最好是在草稿纸上画一画,走一遍流程,凭空想很容易会出错.这一题时LeetCode 25的k=2特殊例子,所以要简单很多.用两个node,一前一后,然后两两交换就好,细节上注意的是交换后两个node前面和后面是否各自连接上.最好自己在纸上走一遍,防止出错. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * public class 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] 24 Game 二十四点游戏

You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, )to get the value of 24. Example 1: Input: [4, 1, 8, 7] Output: True Explanation: (8-4) * (7-1) = 24 Example 2: Input: [1, 2,

(Java) LeetCode 24. 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. Note: Your algorithm should use only constant extra space. You may not modify the values in the

19.1.30 [LeetCode 24] Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

[刷题] LeetCode 24 Swap Nodes in Paris

要求 给定一个链表,对于每两个相邻的节点,交换其位置 示例 1->2->3->4->NULL 2->1->4->3->NULL 实现 1 struct ListNode { 2 int val; 3 ListNode *next; 4 ListNode(int x) : val(x), next(NULL) {} 5 }; 6 7 class Solution { 8 public: 9 ListNode* swapPairs(ListNode* head)

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

[LeetCode]24. Search Insert Position插入位置

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →

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->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 li