![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LinkedList
lviiii
这个作者很懒,什么都没留下…
展开
-
LeetCode Solutions : Remove Nth Node From End of List
Given a linked list,remove the nth node from the end of list and return its head. For example, Given linked list:1->2->3->4->5, and n = 2. After removing the second node from the end原创 2014-08-13 15:41:30 · 522 阅读 · 0 评论 -
LeetCode Solutions : Remove Duplicates from Sorted List II
【编程步骤】 * 1. 处理特殊情况:如果该链表为空或只有一个结点,说明此链表没有重复的元素,直接返回头指针head;即:if(head==null||head.next==null) return head; * 2. 设置伪头结点;即:ListNode pre=new ListNode(0); pre.next=head; head=pre; * 3. 令cur=pre.next,若不存在复本,cur依然为pre原创 2014-11-21 09:03:39 · 716 阅读 · 0 评论 -
LeetCode Solutions : Remove Nth Node From End of List
【算法思路】这道题的关键在于找倒数第n个结点的前驱结点:可以设置慢行指针slow和快行指针fast,让快行指针fast先走n步,然后同步地移动慢行指针slow和快行指针fast,这样它们之间的距离永远相差n个结点,当fast指针移动最后一个结点的时候,slow指向倒数第n个结点;【编程步骤】 * 1. 处理特殊情况:如果被移除的结点为头结点,则移除,并返回第二个结点作为头结点; * 2. 如果为其他情况,则找到倒数第n个结点的前驱结点 pre; * 3. 移除倒数第n个结点,即 pre.next=s原创 2014-11-18 18:29:55 · 606 阅读 · 0 评论 -
LeetCode Solutions : Remove Duplicates from Sorted List
【题目描述】Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.【编程步骤】 * 1. 处理特殊情况:如果该链表为空或只有一个结点,说明此链表没有重复的元素,直接返回头指针head;即:if(head==null||原创 2014-11-19 19:10:08 · 581 阅读 · 0 评论 -
LeetCode Solutions : Sort List
【算法思路】时间复杂度限制在O(n log n),我们可以第一时间想到常用的二路归并排序,快速排序和堆排序,其中快排和堆排只适用于线性表,即数组,故这道编程题毫无疑问用二路归并排序;【编程步骤】 * 1. 利用一个小技巧,可以设置慢行指针low和快行指针fast,把链表分成两部分来操作,即first和second链表 * 2. 递归排序first和second链表,即 first=sortList(head); second=sortList(second); * 3. 合并这两个链表,即:原创 2014-11-18 09:18:20 · 1515 阅读 · 1 评论 -
LeetCode Solutions : 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. Y原创 2014-11-14 11:41:19 · 1012 阅读 · 0 评论 -
LeetCode Solutions : Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL./** * Definition for singly-link原创 2014-11-13 19:17:17 · 844 阅读 · 0 评论 -
LeetCode Solutions : Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL./** * Definition for singly-link原创 2014-09-03 18:54:30 · 760 阅读 · 0 评论 -
LeetCode Solutions : Remove Duplicates from Sorted List I & II
Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, r原创 2014-09-01 17:18:42 · 735 阅读 · 0 评论 -
LeetCode Solutions : Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder itto {1,4,2,3}.Considering the following steps: * 1. split such list原创 2014-11-17 10:01:06 · 978 阅读 · 0 评论