LeetCode-206&92.Reverse Linked List
2016-04-10 22:21
399 查看
206 https://leetcode.com/problems/reverse-linked-list/
Reverse a singly linked list.
92 https://leetcode.com/problems/reverse-linked-list-ii/
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given
return
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
使用栈:(低效)
不用栈
Reverse a singly linked list.
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */ public class Solution { public ListNode ReverseList(ListNode head) { if (head == null || head.next == null) return head; ListNode pre=null, next; while (head != null) { next = head.next; head.next = pre; pre = head; head = next; } return pre; } }递归
public ListNode ReverseList(ListNode head) { if (head == null || head.next == null) return head; ListNode next = head.next; head.next = null; ListNode node = ReverseList(next); next.next = head; return node; }
92 https://leetcode.com/problems/reverse-linked-list-ii/
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given
1->2->3->4->5->NULL, m = 2 and n = 4,
return
1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
使用栈:(低效)
public ListNode ReverseBetween(ListNode head, int m, int n) { if(head.next==null||m==n) return head; ListNode pre = new ListNode(0); pre.next = head; ListNode curNode = pre; Stack<ListNode> stack = new Stack<ListNode>(); for (int i = 0; i < m; i++) { curNode = curNode.next; } for (int i = m; i <= n; i++) { stack.Push(curNode); curNode = curNode.next; } ListNode leftNode = curNode; curNode = pre; for (int i = 0; i < m - 1; i++) { curNode = curNode.next; } while (stack.Count!=0) { curNode.next = stack.Pop(); curNode = curNode.next; } curNode.next = leftNode; return pre.next; }
不用栈
public ListNode ReverseBetween(ListNode head, int m, int n) { if (head.next == null || m == n) return head; ListNode pre = new ListNode(0); pre.next = head; ListNode curNode = pre; for (int i = 0; i < m - 1; i++) { curNode = curNode.next; } ListNode first = curNode.next; ListNode next = first.next; for (int i = m; i < n; i++) { first.next = next.next; next.next = curNode.next; curNode.next = next; next = first.next; } return pre.next; }
相关文章推荐
- [C/C++]反转链表
- C#实现基于链表的内存记事本实例
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- C++利用静态成员或类模板构建链表的方法讲解
- C++实现简单的学生管理系统
- Linux内核链表实现过程
- C++链表倒序实现方法
- C#通过链表实现队列的方法
- C#实现的简单链表类实例
- 找出链表倒数第n个节点元素的二个方法
- Java数据结构之简单链表的定义与实现方法示例
- C语言单循环链表的表示与实现实例详解
- C++实现的链表类实例
- PHP小教程之实现链表
- PHP中模拟链表和链表的基本操作示例
- C语言双向链表的表示与实现实例详解
- js链表操作(实例讲解)
- C语言实现输出链表中倒数第k个节点
- C++语言实现线性表之链表实例
- STL list链表的用法详细解析