LeetCode链表

Posted 梅花五瓣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode链表相关的知识,希望对你有一定的参考价值。

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链表的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode与《代码随想录》链表篇:做题笔记与总结-JavaScript版

Leetcode题解 - 链表简单部分题目代码+思路(21832032062419876)

[LeetCode]反转链表

5-2******* 测试自己的Leetcode链表代码

LeetCode234:回文链表

[算法] leetcode单链表相关题目详解