对于想了解[Leetcode题目]206.ReverseLinkedList的读者,本文将是一篇不可错过的文章,我们将详细介绍leetcodepremium,并且为您提供关于(Easy)Reverse
对于想了解[Leetcode题目]206. Reverse Linked List的读者,本文将是一篇不可错过的文章,我们将详细介绍leetcode premium,并且为您提供关于(Easy) Reverse linked list LeetCode、203. Remove Linked List Elements - LeetCode、206. Reverse Linked List、206. Reverse Linked List - LeetCode的有价值信息。
本文目录一览:- [Leetcode题目]206. Reverse Linked List(leetcode premium)
- (Easy) Reverse linked list LeetCode
- 203. Remove Linked List Elements - LeetCode
- 206. Reverse Linked List
- 206. Reverse Linked List - LeetCode
[Leetcode题目]206. Reverse Linked List(leetcode premium)
Reverse a singly linked list.
click to show more hints.
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
题目翻译:
翻转链表
思路:
1. 循环
另建一个链表,每循环出一个节点,都将该节点加到新链表的表头
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null){
return null;
}
if(head.next==null){
return head;
}
ListNode head1=null;
while(head!=null){
ListNode temp=head;
head=head.next;
temp.next=head1;
head1=temp;
}
return head1;
}
}
2. 递归
从head节点开始,若下一个节点的链表已经反转,那么把该节点移到下一个已反转好的链表末尾就可以了
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode head1=reverseList(head.next);
ListNode result=head1;
while(head1.next!=null){
head1=head1.next;
}
head1.next=head;
head.next=null;
return result;
}
}
递归思路较清晰,但实际可以看出对时间的消耗是要更多了
循环O(n),递归O(nlogn)
更正:犯蠢了,实际上在reverseList之前拿到的next就是reverseList的队尾,不需要再用循环去队尾找,
递归修改如下
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode head1=head.next;
ListNode result=reverseList(head.next);
head1.next=head;
head.next=null;
return result;
}
}
(Easy) Reverse linked list LeetCode
Description:
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?
Solution:
Explanation:
Iterative Method
- Initialize three pointers prev as NULL,curr as head and next as NULL.
- Iterate trough the linked list. In loop,do following.
// Before changing next of current,
// store next node
next = curr->next// Now change next of current
// This is where actual reversing happens
curr->next = prev// Move prev and curr one step forward
prev = curr
curr = next
/** * DeFinition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode reverseList(ListNode head) { ListNode prev =null,curr = head,next = null; while(curr!=null){ next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; } }
203. Remove Linked List Elements - LeetCode
Question
203. Remove Linked List Elements
Solution
题目大意:从链表中删除给定的数
思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点
Java实现:
public ListNode removeElements(ListNode head, int val) {
ListNode cur = head;
while (cur != null) {
if (cur.next != null && cur.next.val == val) {
ListNode tmp = cur.next.next;
cur.next = tmp;
} else {
cur = cur.next;
}
}
return (head != null && head.val == val) ? head.next : head;
}
206. Reverse Linked List
206. Reverse Linked List
Reverse a singly linked list.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null)
return head;
ListNode pReverseHead=null;
ListNode pPre=null;
ListNode pNode=head;
while(pNode!=null){
ListNode pPost=pNode.next;
if(pPost==null)
pReverseHead=pNode;
pNode.next=pPre;
pPre=pNode;
pNode=pPost;
}
return pReverseHead;
}
}
206. Reverse Linked List - LeetCode
Question
206. Reverse Linked List
Solution
题目大意:对一个链表进行反转
思路:
Java 实现:
public ListNode reverseList(ListNode head) {
ListNode newHead = null;
while (head != null) {
ListNode tmp = head.next;
head.next = newHead;
newHead = head;
head = tmp;
}
return newHead;
}
今天关于[Leetcode题目]206. Reverse Linked List和leetcode premium的分享就到这里,希望大家有所收获,若想了解更多关于(Easy) Reverse linked list LeetCode、203. Remove Linked List Elements - LeetCode、206. Reverse Linked List、206. Reverse Linked List - LeetCode等相关知识,可以在本站进行查询。
本文标签: