Skip to content

Commit 00e27e4

Browse files
committed
Linked List tag
1 parent 2682a8b commit 00e27e4

11 files changed

+599
-0
lines changed

LinkedList/LinkedList.Lib/CycleSln.cs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* ==============================================================================
2+
* 功能描述:Cycle
3+
* 创 建 者:gz
4+
* 创建日期:2017/4/28 18:38:30
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
//Given a linked list, determine if it has a cycle in it.
11+
namespace LinkedList.Lib
12+
{
13+
/// <summary>
14+
/// Cycle
15+
/// </summary>
16+
public class CycleSln
17+
{
18+
19+
20+
#region 公有方法
21+
public bool HasCycle(ListNode head)
22+
{
23+
if (head == null) return false;
24+
if (head.next == null) return false;
25+
if (head.next.next == null) return false;
26+
27+
ListNode slow = head;
28+
ListNode fast = head;
29+
30+
while (fast != null && fast.next != null)
31+
{
32+
slow = slow.next;
33+
fast = fast.next.next;
34+
if (fast == null)
35+
return false; //快指针如果为null,不存在环
36+
if (fast.val == slow.val)
37+
{
38+
return true; //找到节点数据域相等点,存在环
39+
}
40+
}
41+
return false;
42+
}
43+
#endregion
44+
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{A900519C-FE07-48E6-AC05-ACBA9FEB7927}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>LinkedList.Lib</RootNamespace>
12+
<AssemblyName>LinkedList.Lib</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Xml" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="CycleSln.cs" />
44+
<Compile Include="LinkesListBasic.cs" />
45+
<Compile Include="ListNode.cs" />
46+
<Compile Include="PalindromeLinkedList.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
50+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
51+
Other similar extension points exist, see Microsoft.Common.targets.
52+
<Target Name="BeforeBuild">
53+
</Target>
54+
<Target Name="AfterBuild">
55+
</Target>
56+
-->
57+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/* ==============================================================================
2+
* 功能描述:LinkesListBasic
3+
* 创 建 者:wack
4+
* 创建日期:2017/4/28 18:42:06
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
11+
namespace LinkedList.Lib
12+
{
13+
/// <summary>
14+
/// LinkesListBasic
15+
/// </summary>
16+
public class LinkesListBasic
17+
{
18+
#region 构造函数
19+
20+
public LinkesListBasic()
21+
{
22+
}
23+
24+
#endregion
25+
26+
#region 属性字段
27+
28+
#endregion
29+
30+
#region 私有方法
31+
32+
#endregion
33+
34+
#region 公有方法
35+
//Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
36+
//Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3,
37+
//the linked list should become 1 -> 2 -> 4 after calling your function.
38+
public void DeleteNode(ListNode node)
39+
{
40+
if (node == null)
41+
return;
42+
node.val = node.next.val;
43+
node.next = node.next.next;
44+
}
45+
46+
//Given a sorted linked list, delete all duplicates such that each element appear only once.
47+
//For example,
48+
//Given 1->1->2, return 1->2.
49+
//Given 1->1->2->3->3, return 1->2->3.
50+
public ListNode DeleteDuplicates(ListNode head)
51+
{
52+
ListNode diff = head;
53+
ListNode tmp = head;
54+
while (tmp != null && tmp.next != null)
55+
{
56+
while (tmp.next != null && tmp.next.val == tmp.val)
57+
{
58+
tmp = tmp.next;
59+
}
60+
diff.next = tmp.next;//找到一个新值
61+
diff = diff.next;//迭代
62+
tmp = diff;
63+
}
64+
65+
return head;
66+
}
67+
68+
// Write a program to find the node at which the intersection of two singly linked lists begins.
69+
//For example, the following two linked lists:
70+
//A: a1 → a2
71+
// ↘
72+
// c1 → c2 → c3
73+
// ↗
74+
//B: b1 → b2 → b3
75+
//begin to intersect at node c1.
76+
public ListNode GetIntersectionNode(ListNode headA, ListNode headB)
77+
{
78+
if (headA == null || headB == null)
79+
return null;
80+
ListNode a = headA;
81+
ListNode b = headB;
82+
while (a != b)
83+
{
84+
if (a == null)
85+
a = headB;
86+
else
87+
{
88+
a = a.next;
89+
}
90+
if (b == null)
91+
b = headA;
92+
else
93+
{
94+
b = b.next;
95+
}
96+
97+
}
98+
return a;
99+
}
100+
101+
//Remove all elements from a linked list of integers that have value val.
102+
//Example
103+
//Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
104+
//Return: 1 --> 2 --> 3 --> 4 --> 5
105+
public ListNode RemoveElements(ListNode head, int val)
106+
{
107+
if (head == null)
108+
return null;
109+
//检测头是否等于val
110+
if (head.val == val)
111+
head = head.next;
112+
ListNode tmp = head;
113+
while (tmp != null && tmp.next != null)
114+
{
115+
if (tmp.next.val == val)
116+
{
117+
deleteNode(tmp, tmp.next);
118+
}
119+
else
120+
tmp = tmp.next;
121+
}
122+
//检测头是否等于val
123+
if (head != null && head.val == val)
124+
head = head.next;
125+
return head;
126+
}
127+
128+
//删除节点node,pre为node的前驱
129+
private void deleteNode(ListNode pre, ListNode node)
130+
{
131+
ListNode tmp = node.next;
132+
pre.next = tmp;
133+
}
134+
135+
136+
//Reverse a singly linked list.
137+
public ListNode ReverseList(ListNode head)
138+
{
139+
if (head == null || head.next == null)
140+
return head;
141+
ListNode a = head;
142+
ListNode b = head.next;
143+
a.next = null;
144+
while (b != null)
145+
{
146+
ListNode tmp = b.next; //保存节点b后的所有节点顺序
147+
b.next = a; //上步保存后,可以放心的将b的next域指向a,实现反转
148+
a = b; //上步实现反转后,再赋值给a,这样a始终为反转链表的头节点
149+
b = tmp;//上步后实现了反转,这步实现迭代,即让b再在原来的链表中保持前行。
150+
}
151+
return a;
152+
}
153+
154+
//Merge two sorted linked lists and return it as a new list.
155+
//The new list should be made by splicing together the nodes of the first two lists.
156+
public ListNode MergeTwoLists(ListNode l1, ListNode l2)
157+
{
158+
if (l1 == null) return l2;
159+
if (l2 == null) return l1;
160+
161+
ListNode merge;
162+
//首先确定merge的头节点指向谁
163+
if (l1.val < l2.val)
164+
{
165+
merge = l1;
166+
l1 = l1.next;
167+
}
168+
else
169+
{
170+
merge = l2;
171+
l2 = l2.next;
172+
}
173+
174+
ListNode im = merge; //临时指针指向merge
175+
while (l1 != null)
176+
{
177+
if (l2 != null)
178+
{
179+
if (l1.val < l2.val)
180+
{
181+
im.next = l1;
182+
l1 = l1.next;
183+
}
184+
else
185+
{
186+
im.next = l2;
187+
l2 = l2.next;
188+
}
189+
im = im.next;
190+
}
191+
else //b首先等于null
192+
{
193+
im.next = l1;
194+
break;
195+
}
196+
}
197+
198+
if (l2 != null) //a首先等于null
199+
im.next = l2;
200+
201+
return merge;
202+
203+
}
204+
205+
#endregion
206+
207+
}
208+
}

LinkedList/LinkedList.Lib/ListNode.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* ==============================================================================
2+
* 功能描述:ListNode
3+
* 创 建 者:gz
4+
* 创建日期:2017/4/28 18:39:50
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
11+
namespace LinkedList.Lib
12+
{
13+
/// <summary>
14+
/// ListNode
15+
/// </summary>
16+
public class ListNode
17+
{
18+
#region 构造函数
19+
20+
public ListNode(int val)
21+
{
22+
this.val = val;
23+
}
24+
25+
#endregion
26+
27+
#region 属性字段
28+
public int val { get; set; }
29+
public ListNode next { get; set; }
30+
#endregion
31+
32+
#region 私有方法
33+
34+
#endregion
35+
36+
#region 公有方法
37+
38+
#endregion
39+
40+
}
41+
}

0 commit comments

Comments
 (0)