码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】链表

时间:2020-06-29 22:41:37      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:connect   set   hang   elf   only   strong   content   ever   reverse   

24. Swap Nodes in Pairs

https://leetcode.com/problems/linked-list-cycle/

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->4->3.
C 实现:
 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 
 9 struct ListNode* swapPairs(struct ListNode* head){
10     if(head == NULL || head->next == NULL)
11         return head;
12     
13     struct ListNode* pNode = head;
14     
15     while(pNode && pNode->next)
16     {
17         int val = 0;
18         
19         val = pNode->val;
20         pNode->val = pNode->next->val;
21         pNode->next->val = val;
22         
23         pNode = pNode->next->next;
24     }
25     return head;
26 }

C++实现:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode() : val(0), next(nullptr) {}
 7  *     ListNode(int x) : val(x), next(nullptr) {}
 8  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 9  * };
10  */
11 class Solution {
12 public:
13     ListNode* swapPairs(ListNode* head) {
14         ListNode* pNode = head;
15         
16         if(head == NULL || head->next == NULL)
17             return head;
18         
19         while(pNode && pNode->next)
20         {
21             int iVal = 0;
22             
23             iVal = pNode->val;
24             pNode->val = pNode->next->val;
25             pNode->next->val = iVal;
26             
27             pNode = pNode->next->next;
28         }
29         return head;
30     }
31 };

 

 

141. Linked List Cycle

https://leetcode.com/problems/linked-list-cycle/

Given a linked list, determine if it has a cycle in it.

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

 

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.

技术图片

Example 2:

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.

技术图片

Example 3:

Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.

技术图片

 

Follow up:

Can you solve it using O(1) (i.e. constant) memory?

C 实现:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 bool hasCycle(struct ListNode *head) {
 9     if(head == NULL || head->next == NULL)
10         return false;
11     
12     struct ListNode *pLast = head;
13     struct ListNode *pFast = head->next;
14     
15     while(pFast && pFast->next)
16     {
17         if(pFast == pLast)
18             return true;
19         pLast = pLast->next;
20         pFast = pFast->next->next;
21     }
22     return false;
23 }

C++实现:

 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     bool hasCycle(ListNode *head) {
12         if(!head || !head->next)
13         {
14             return false;
15         }
16         ListNode *pLast = head;
17         ListNode *pFast = head->next;    
18         
19         while(pFast && pFast->next)
20         {
21             if(pLast == pFast)
22             {
23                 return true;
24             }
25             pLast = pLast->next;
26             pFast = pFast->next->next;
27         }
28         return false;
29     }
30 };

 

 

206. Reverse Linked List

https://leetcode.com/problems/linked-list-cycle/

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?

C 实现:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 
 9 
10 struct ListNode* reverseList(struct ListNode* head){
11     struct ListNode* pCur = head;
12     struct ListNode* pRec = NULL;
13     
14     while(pCur)
15     {
16         struct ListNode* pTem = NULL;
17         
18         pTem = pCur->next;
19         pCur->next = pRec;
20         pRec = pCur;
21         pCur = pTem;
22     }
23     return pRec;
24 }

C++实现:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode() : val(0), next(nullptr) {}
 7  *     ListNode(int x) : val(x), next(nullptr) {}
 8  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 9  * };
10  */
11 class Solution {
12 public:
13     ListNode* reverseList(ListNode* head) {
14         ListNode *pNode = head;
15         ListNode *pRec = NULL;
16         
17         while(pNode)
18         {
19             ListNode *pTem = NULL;
20             
21             pTem = pNode->next;
22             pNode->next = pRec;
23             pRec = pNode;
24             pNode = pTem;
25         }
26         return pRec;
27     }
28 };

 

 

 

【LeetCode】链表

标签:connect   set   hang   elf   only   strong   content   ever   reverse   

原文地址:https://www.cnblogs.com/meihuawuban/p/13207261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!