From 1e1d204333f3e4c8e2a497ea4c893cb5d01b3496 Mon Sep 17 00:00:00 2001 From: Wenru Dong Date: Sat, 6 Oct 2018 22:24:35 +0100 Subject: [PATCH 01/10] implementation of linked list in objective-c --- object-c/06_linkedlist/ListNode.h | 19 +++ object-c/06_linkedlist/ListNode.m | 28 ++++ object-c/06_linkedlist/SinglyLinkedList.h | 29 ++++ object-c/06_linkedlist/SinglyLinkedList.m | 117 ++++++++++++++++ .../06_linkedlist/SinglyLinkedListTests.m | 126 ++++++++++++++++++ 5 files changed, 319 insertions(+) create mode 100644 object-c/06_linkedlist/ListNode.h create mode 100644 object-c/06_linkedlist/ListNode.m create mode 100644 object-c/06_linkedlist/SinglyLinkedList.h create mode 100644 object-c/06_linkedlist/SinglyLinkedList.m create mode 100644 object-c/06_linkedlist/SinglyLinkedListTests.m diff --git a/object-c/06_linkedlist/ListNode.h b/object-c/06_linkedlist/ListNode.h new file mode 100644 index 00000000..2e5418b7 --- /dev/null +++ b/object-c/06_linkedlist/ListNode.h @@ -0,0 +1,19 @@ +// +// ListNode.h +// algo +// +// Created by Wenru Dong on 2018/10/6. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import + +@interface ListNode : NSObject + +@property int value; +@property ListNode *next; + +- (instancetype)initWithValue:(int)value; ++ (instancetype)nodeWithValue:(int)value; + +@end diff --git a/object-c/06_linkedlist/ListNode.m b/object-c/06_linkedlist/ListNode.m new file mode 100644 index 00000000..93d562d2 --- /dev/null +++ b/object-c/06_linkedlist/ListNode.m @@ -0,0 +1,28 @@ +// +// ListNode.m +// algo +// +// Created by Wenru Dong on 2018/10/6. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import "ListNode.h" + +@implementation ListNode + +- (instancetype)initWithValue:(int)value { + if (self = [super init]) { + _value = value; + } + return self; +} + ++ (instancetype)nodeWithValue:(int)value { + return [[self alloc] initWithValue:value]; +} + +- (NSString*)debugDescription { + return [NSString stringWithFormat:@"%d", _value]; +} + +@end diff --git a/object-c/06_linkedlist/SinglyLinkedList.h b/object-c/06_linkedlist/SinglyLinkedList.h new file mode 100644 index 00000000..c3866d5b --- /dev/null +++ b/object-c/06_linkedlist/SinglyLinkedList.h @@ -0,0 +1,29 @@ +// +// SinglyLinkedList.h +// algo +// +// Created by Wenru Dong on 2018/10/6. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import +#import "ListNode.h" + +@interface SinglyLinkedList : NSObject + +@property ListNode* head; + +- (ListNode*)nodeWithValue:(int)value; +- (ListNode*)nodeAtIndex:(NSUInteger)index; + +- (void)insertNodeWithValue:(int)value; +- (void)insertNode:(nonnull ListNode*)node; ++ (void)insertNodeWithValue:(int)value afterNode:(nonnull ListNode*)node; ++ (void)insertNode:(nonnull ListNode*)aNode afterNode:(nonnull ListNode*)node; +- (void)insertNodeWithValue:(int)value beforeNode:(nonnull ListNode*)node; +- (void)insertNode:(nonnull ListNode*)aNode beforeNode:(nonnull ListNode*)node; + +- (void)deleteNode:(nonnull ListNode*)node; +- (void)deleteNodesWithValue:(int)value; + +@end diff --git a/object-c/06_linkedlist/SinglyLinkedList.m b/object-c/06_linkedlist/SinglyLinkedList.m new file mode 100644 index 00000000..52e663a5 --- /dev/null +++ b/object-c/06_linkedlist/SinglyLinkedList.m @@ -0,0 +1,117 @@ +// +// SinglyLinkedList.m +// algo +// +// Created by Wenru Dong on 2018/10/6. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import "SinglyLinkedList.h" + +@implementation SinglyLinkedList + +- (ListNode*)nodeWithValue:(int)value { + ListNode* current = _head; + while (current && current.value != value) { + current = current.next; + } + return current; +} + +- (ListNode*)nodeAtIndex:(NSUInteger)index { + ListNode* current = _head; + NSUInteger position = 0; + while (current && position != index) { + current = current.next; + position++; + } + return current; +} + +- (void)insertNodeWithValue:(int)value { + ListNode* aNode = [ListNode nodeWithValue:value]; + [self insertNode:aNode]; +} + +- (void)insertNode:(nonnull ListNode *)node { + node.next = _head; + _head = node; +} + ++ (void)insertNodeWithValue:(int)value afterNode:(nonnull ListNode *)node { + ListNode* aNode = [ListNode nodeWithValue:value]; + [SinglyLinkedList insertNode:aNode afterNode:node]; +} + ++ (void)insertNode:(nonnull ListNode *)aNode afterNode:(nonnull ListNode *)node { + aNode.next = node.next; + node.next = aNode; +} + +- (void)insertNodeWithValue:(int)value beforeNode:(nonnull ListNode *)node { + ListNode* aNode = [ListNode nodeWithValue:value]; + [self insertNode:aNode beforeNode:node]; +} + +- (void)insertNode:(nonnull ListNode *)aNode beforeNode:(nonnull ListNode *)node { + ListNode* fakeHead = [ListNode nodeWithValue:0]; + fakeHead.next = _head; + ListNode* current = fakeHead; + while (current.next && current.next != node) { + current = current.next; + } + if (current.next == nil) { + return; + } + aNode.next = node; + current.next = aNode; +} + +- (void)deleteNode:(nonnull ListNode *)node { + if (node.next) { + node.value = node.next.value; + node.next = node.next.next; + return; + } + if (_head == nil) return; + ListNode* current = _head; + while (current.next && current.next != node) { + current = current.next; + } + current.next = nil; +} + +- (void)deleteNodesWithValue:(int)value { + ListNode* fakeHead = [ListNode nodeWithValue:value+1]; + fakeHead.next = _head; + ListNode* prev = fakeHead; + ListNode* current = _head; + while (current) { + if (current.value != value) { + prev.next = current; + prev = prev.next; + } + current = current.next; + } + if (prev.next) { + prev.next = nil; + } + _head = fakeHead.next; +} + +- (NSString*)debugDescription { + NSMutableString* info = [[NSMutableString alloc] init]; + ListNode* current = _head; + if (current) { + [info appendString:current.debugDescription]; + } + current = current.next; + while (current) { + [info appendString:@"->"]; + [info appendString:current.debugDescription]; + current = current.next; + } + return [NSString stringWithString:info]; +} + +@end diff --git a/object-c/06_linkedlist/SinglyLinkedListTests.m b/object-c/06_linkedlist/SinglyLinkedListTests.m new file mode 100644 index 00000000..640614e4 --- /dev/null +++ b/object-c/06_linkedlist/SinglyLinkedListTests.m @@ -0,0 +1,126 @@ +// +// SinglyLinkedListTests.m +// SinglyLinkedListTests +// +// Created by Wenru Dong on 2018/10/6. +// Copyright © 2018年 Wenru Dong. All rights reserved. +// + +#import +#import "ListNode.h" +#import "SinglyLinkedList.h" + +@interface SinglyLinkedListTests : XCTestCase + +@end + +@implementation SinglyLinkedListTests +{ + SinglyLinkedList* _list; + NSArray* _nodes; +} +- (void)setUp { + [super setUp]; + ListNode* node1 = [ListNode nodeWithValue:1]; + ListNode* node2 = [ListNode nodeWithValue:2]; + ListNode* node3 = [ListNode nodeWithValue:3]; + ListNode* node4 = [ListNode nodeWithValue:4]; + ListNode* node5 = [ListNode nodeWithValue:5]; + ListNode* node6 = [ListNode nodeWithValue:6]; + node1.next = node2; + node2.next = node3; + node3.next = node4; + node4.next = node5; + node5.next = node6; + + _list = [[SinglyLinkedList alloc] init]; + _list.head = node1; + _nodes = [NSArray arrayWithObjects:node1, node2, node3, node4, node5, node6, nil]; +} + +- (void)tearDown { + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +- (void)testNodeWithValue { + XCTAssertEqualObjects([_list nodeWithValue:1], _list.head); + XCTAssertNil([_list nodeWithValue:10]); +} + +- (void)testNodeAtIndex { + XCTAssertEqualObjects([_list nodeAtIndex:4], _nodes[4]); + XCTAssertNil([_list nodeAtIndex:10]); +} + +- (void)testInsertNodeWithValue { + [_list insertNodeWithValue:9]; + XCTAssertEqual(_list.head.value, 9); + XCTAssertEqual(_list.head.next.value, 1); +} + +- (void)testInsertNode { + ListNode* aNode = [ListNode nodeWithValue:7]; + [_list insertNode:aNode]; + XCTAssertEqualObjects(_list.head, aNode); +} + +- (void)testInsertNodeWithValueAfterNode { + [SinglyLinkedList insertNodeWithValue:12 afterNode:_nodes[3]]; + XCTAssertEqual([[_list nodeAtIndex:4] value], 12); +} + +- (void)testInsertNodeAfterNode { + ListNode* aNode = [ListNode nodeWithValue:28]; + [SinglyLinkedList insertNode:aNode afterNode:_nodes[5]]; + ListNode* prevNode = (ListNode *)_nodes[5]; + XCTAssertEqualObjects(aNode, prevNode.next); +} + +- (void)testInsertNodeBeforeNode { + ListNode* aNode = [ListNode nodeWithValue:27]; + ListNode* prevNode = (ListNode *)_nodes[3]; + [_list insertNode:aNode beforeNode:_nodes[4]]; + XCTAssertEqualObjects(aNode, prevNode.next); +} + +- (void)testInsertNodeBeforeUnconnectedNode { + ListNode* aNode = [ListNode nodeWithValue:27]; + ListNode* floatingNode = [ListNode nodeWithValue:36]; + [_list insertNode:aNode beforeNode:floatingNode]; + for (NSUInteger i = 0; i < 6; i++) { + XCTAssertEqualObjects([_list nodeAtIndex:i], _nodes[i]); + } +} + +- (void)testDeleteNode { + [_list deleteNode:_nodes[0]]; + XCTAssertEqual(_list.head.value, 2); + [_list deleteNode:_nodes[5]]; + ListNode* lastNode = (ListNode *)_nodes[4]; + XCTAssertNil(lastNode.next); +} + +- (void)testDeleteNodesWithValue { + ListNode* firstNode = [ListNode nodeWithValue:1]; + ListNode* secondNode = [ListNode nodeWithValue:1]; + [_list insertNode:firstNode]; + [_list insertNode:secondNode]; + [_list deleteNodesWithValue:1]; + for (NSUInteger i = 1; i < 6; i++) { + XCTAssertEqualObjects([_list nodeAtIndex:i-1], _nodes[i]); + } +} + +- (void)testDebugDescription { + XCTAssertEqualObjects(_list.debugDescription, @"1->2->3->4->5->6"); +} + +//- (void)testPerformanceExample { +// // This is an example of a performance test case. +// [self measureBlock:^{ +// // Put the code you want to measure the time of here. +// }]; +//} + +@end From 3551b0820a1148054a6467924561aeeecd4e9beb Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 7 Oct 2018 10:04:37 +0800 Subject: [PATCH 02/10] 07_linkedlist --- go/07_linkedlist/main.go | 146 ++++++++++++++++++++++++++++++++++ go/07_linkedlist/main_test.go | 59 ++++++++++++++ 2 files changed, 205 insertions(+) create mode 100644 go/07_linkedlist/main.go create mode 100644 go/07_linkedlist/main_test.go diff --git a/go/07_linkedlist/main.go b/go/07_linkedlist/main.go new file mode 100644 index 00000000..51444036 --- /dev/null +++ b/go/07_linkedlist/main.go @@ -0,0 +1,146 @@ +package _7_linkedlist + +import "fmt" + +//单链表节点 +type ListNode struct { + next *ListNode + value interface{} +} + +//单链表 +type LinkedList struct { + head *ListNode +} + +//打印链表 +func (this *LinkedList) Print() { + cur := this.head.next + format := "" + for nil != cur { + format += fmt.Sprintf("%+v", cur.value) + cur = cur.next + if nil != cur { + format += "->" + } + } + fmt.Println(format) +} + +/* +单链表反转 +时间复杂度:O(N) +*/ +func (this *LinkedList) Reverse() { + if nil == this.head || nil == this.head.next || nil == this.head.next.next { + return + } + + var pre *ListNode = nil + cur := this.head.next + for nil != cur { + tmp := cur.next + cur.next = pre + pre = cur + cur = tmp + } + + this.head.next = pre +} + +/* +判断单链表是否有环 +*/ +func (this *LinkedList) HasCycle() bool { + if nil != this.head { + slow := this.head + fast := this.head + for nil != fast && nil != fast.next { + slow = slow.next + fast = fast.next.next + if slow == fast { + return true + } + } + } + return false +} + +/* +两个有序单链表合并 +*/ +func MergeSortedList(l1, l2 *LinkedList) *LinkedList { + if nil == l1 || nil == l1.head || nil == l1.head.next { + return l2 + } + if nil == l2 || nil == l2.head || nil == l2.head.next { + return l1 + } + + l := &LinkedList{head: &ListNode{}} + cur := l.head + curl1 := l1.head.next + curl2 := l2.head.next + for nil != curl1 && nil != curl2 { + if curl1.value.(int) > curl2.value.(int) { + cur.next = curl2 + curl2 = curl2.next + } else { + cur.next = curl1 + curl1 = curl1.next + } + cur = cur.next + } + + if nil != curl1 { + cur.next = curl1 + } else if nil != curl2 { + cur.next = curl2 + } + + return l +} + +/* +删除倒数第N个节点 +*/ +func (this *LinkedList) DeleteBottomN(n int) { + if n <= 0 || nil == this.head || nil == this.head.next { + return + } + + fast := this.head + for i := 1; i <= n && fast != nil; i++ { + fast = fast.next + } + + if nil == fast { + return + } + + slow := this.head + for nil != fast.next { + slow = slow.next + fast = fast.next + } + slow.next = slow.next.next +} + +/* +获取中间节点 +*/ +func (this *LinkedList) FindMiddleNode() *ListNode { + if nil == this.head || nil == this.head.next { + return nil + } + if nil == this.head.next.next { + return this.head.next + } + + slow, fast := this.head, this.head + for nil != fast && nil != fast.next { + slow = slow.next + fast = fast.next.next + } + return slow +} diff --git a/go/07_linkedlist/main_test.go b/go/07_linkedlist/main_test.go new file mode 100644 index 00000000..82180421 --- /dev/null +++ b/go/07_linkedlist/main_test.go @@ -0,0 +1,59 @@ +package _7_linkedlist + +import "testing" + +var l *LinkedList + +func init() { + n5 := &ListNode{value: 5} + n4 := &ListNode{value: 4, next: n5} + n3 := &ListNode{value: 3, next: n4} + n2 := &ListNode{value: 2, next: n3} + n1 := &ListNode{value: 1, next: n2} + l = &LinkedList{head: &ListNode{next: n1}} +} + +func TestReverse(t *testing.T) { + l.Print() + l.Reverse() + l.Print() +} + +func TestHasCycle(t *testing.T) { + t.Log(l.HasCycle()) + l.head.next.next.next.next.next.next = l.head.next.next.next + t.Log(l.HasCycle()) +} + +func TestMergeSortedList(t *testing.T) { + n5 := &ListNode{value: 9} + n4 := &ListNode{value: 7, next: n5} + n3 := &ListNode{value: 5, next: n4} + n2 := &ListNode{value: 3, next: n3} + n1 := &ListNode{value: 1, next: n2} + l1 := &LinkedList{head: &ListNode{next: n1}} + + n10 := &ListNode{value: 10} + n9 := &ListNode{value: 8, next: n10} + n8 := &ListNode{value: 6, next: n9} + n7 := &ListNode{value: 4, next: n8} + n6 := &ListNode{value: 2, next: n7} + l2 := &LinkedList{head: &ListNode{next: n6}} + + MergeSortedList(l1, l2).Print() +} + +func TestDeleteBottomN(t *testing.T) { + l.Print() + l.DeleteBottomN(3) + l.Print() +} + +func TestFindMiddleNode(t *testing.T) { + l.DeleteBottomN(1) + l.DeleteBottomN(1) + l.DeleteBottomN(1) + l.DeleteBottomN(1) + l.Print() + t.Log(l.FindMiddleNode()) +} From 62a07df093e8ae8729a2418f3c827ea0729ad8ea Mon Sep 17 00:00:00 2001 From: xumorden <748838076@qq.com> Date: Sun, 7 Oct 2018 11:32:14 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E6=82=A8=E5=AE=9A=E4=B9=89=E7=9A=84n?= =?UTF-8?q?=E5=92=8Ccount=E6=B2=A1=E6=9C=89=E7=9C=8B=E6=87=82=EF=BC=8C?= =?UTF-8?q?=E8=80=8C=E4=B8=94=E8=B0=83=E7=94=A8=E8=BF=87=E7=A8=8B=E4=B8=AD?= =?UTF-8?q?=E4=BA=A7=E7=94=9F=E8=A7=92=E6=A0=87=E8=B6=8A=E7=95=8C=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=EF=BC=8C=E6=88=91=E4=BD=BF=E7=94=A8=E7=9A=84JDK?= =?UTF-8?q?=E6=98=AF1.8=E3=80=82=E8=87=AA=E5=B7=B1=E7=A7=81=E4=B8=8B?= =?UTF-8?q?=E6=94=B9=E4=BA=86=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=EF=BC=8C=E5=B8=8C=E6=9C=9B=E8=83=BD=E7=BB=99?= =?UTF-8?q?=E6=82=A8=E7=9A=84=E5=BC=80=E6=BA=90=E9=A1=B9=E7=9B=AE=E5=81=9A?= =?UTF-8?q?=E8=B4=A1=E7=8C=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java/05_array/Array.java | 116 ++++++++++++++++++++++++--------------- 1 file changed, 73 insertions(+), 43 deletions(-) diff --git a/java/05_array/Array.java b/java/05_array/Array.java index 81039510..6b7ad055 100644 --- a/java/05_array/Array.java +++ b/java/05_array/Array.java @@ -7,53 +7,83 @@ * Author: Zheng */ public class Array { - private int data[]; - private int n; - private int count; - - public Array(int capacity) { - data = new int[capacity]; - n = capacity; - count = 0; - } - - public int find(int index) { - if (index < 0 || index >= count ) return -1; - return data[index]; - } - - public boolean delete(int index) { - if (index < 0 || index >= count) return false; - for(int i = index + 1; i < count; ++i) { - data[i-1] = data[i]; + //定义整型数据data保存数据 + public int data[]; + //定义数组长度 + private int n; + //定义中保存的数据个数 + private int count; + + //构造方法,定义数组大小 + public Array(int capacity){ + this.data = new int[]{0,1,2,3,4}; + this.n = capacity; + this.count=capacity; + } + + //根据索引,找到数据中的元素并返回 + public int find(int index){ + if (index<0 || index>=count) return -1; + return data[index]; + } + + //根据索引,删除数组中元素 + public boolean delete(int index){ + if (index<0 || index >=count) return false; + //从删除位置开始,将后面的元素向前移动一位 + for (int i=index+1; i= count) return false; - if (count == n) return false; + //向数组中插入一个元素 + public boolean insert(int index, int value){ + if (index<0 || index>=count) return false; +// if (count == n) return false;不是太懂 + //数组长度增加1 + int[] arr = new int[count+1]; + for (int i = 0; i < data.length; i++) { + arr[i] = data[i]; + } + data=arr; + + for (int i = count-1; i>=index; --i){ + data[i+1] = data[i]; + } + data[index] = value; + ++count; + return true; + } - for (int i = count - 1; i >= index; --i) { - data[i+1] = data[i]; + public boolean insertToTail(int value) { +// if (count == n) return false;不是太懂 + //数组长度增加1 + int[] arr = new int[count+1]; + for (int i = 0; i < data.length; i++) { + arr[i] = data[i]; + } + data=arr; + data[count++] = value; + return true; } - data[index] = value; - ++count; - return true; - } - - public boolean insertToTail(int value) { - if (count == n) return false; - data[count++] = value; - return true; - } - - public void printAll() { - for (int i = 0; i < count; ++i) { - System.out.print(data[i] + " "); + + public void printAll() { + for (int i = 0; i < count; ++i) { + System.out.print(data[i] + " "); + } + System.out.println(); } - System.out.println(); - } + + } From 62636dccffeb74822c462e2f7da438a6ed7960a8 Mon Sep 17 00:00:00 2001 From: wangzheng Date: Sun, 7 Oct 2018 14:51:02 +0800 Subject: [PATCH 04/10] 08_stack --- java/08_stack/StackBasedLinkedList.java | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 java/08_stack/StackBasedLinkedList.java diff --git a/java/08_stack/StackBasedLinkedList.java b/java/08_stack/StackBasedLinkedList.java new file mode 100644 index 00000000..b078d372 --- /dev/null +++ b/java/08_stack/StackBasedLinkedList.java @@ -0,0 +1,54 @@ +package stack; + +/** + * 基于链表实现的栈。 + * + * Author: Zheng + */ +public class StackBasedLinkedList { + private Node top = null; + + public void push(int value) { + Node newNode = new Node(value, null); + // 判断是否栈空 + if (top == null) { + top = newNode; + } else { + newNode.next = top; + top = newNode; + } + } + + /** + * 我用-1表示栈中没有数据。 + */ + public int pop() { + if (top == null) return -1; + int value = top.data; + top = top.next; + return value; + } + + public void printAll() { + Node p = top; + while (p != null) { + System.out.print(p.data + " "); + p = p.next; + } + System.out.println(); + } + + private static class Node { + private int data; + private Node next; + + public Node(int data, Node next) { + this.data = data; + this.next = next; + } + + public int getData() { + return data; + } + } +} From 11d02a3ed049e9a26ccfe73b446df084acb8e6a1 Mon Sep 17 00:00:00 2001 From: Smallfly Date: Sun, 7 Oct 2018 16:31:47 +0800 Subject: [PATCH 05/10] Add C implement for 07_linkedlist --- c-cpp/07_linkedlist/LinkedListAlgo.c | 263 +++++++++++++++++++++++++++ 1 file changed, 263 insertions(+) create mode 100644 c-cpp/07_linkedlist/LinkedListAlgo.c diff --git a/c-cpp/07_linkedlist/LinkedListAlgo.c b/c-cpp/07_linkedlist/LinkedListAlgo.c new file mode 100644 index 00000000..e35d612a --- /dev/null +++ b/c-cpp/07_linkedlist/LinkedListAlgo.c @@ -0,0 +1,263 @@ +#include +#include + +/** + * 1) 单链表反转 + * 2) 链表中环的检测 + * 3) 两个有序的链表合并 + * 4) 删除链表倒数第 n 个结点 + * 5) 求链表的中间结点 + * + * Author: Smallfly + */ + +typedef struct SinglyLinkedNode { + int data; + struct SinglyLinkedNode* next; +} SinglyLinkedNode; + +void insertNode(SinglyLinkedNode** head_ref, int data); +void printLinkedList(SinglyLinkedNode* head); + +/** 反转单链表 */ + +void reverse(SinglyLinkedNode** head_ref) { + if (*head_ref == NULL) return; + + SinglyLinkedNode *prev = NULL; + SinglyLinkedNode *current = *head_ref; + while (current) { + SinglyLinkedNode *next = current->next; + if (!next) { + // 到达尾结点时,将地址存入 head_ref + *head_ref = current; + } + current->next = prev; + prev = current; + current = next; + } +} + +void test_reverse() { + SinglyLinkedNode* head = NULL; + insertNode(&head, 3); + insertNode(&head, 2); + insertNode(&head, 1); + + reverse(&head); + printLinkedList(head); +} + +/** 检测单链表是否有环 */ + +// 这里使用一级指针也可以 +int checkCircle(SinglyLinkedNode** head_ref) { + if (*head_ref == NULL) return 0; + SinglyLinkedNode *slow = *head_ref, *fast = *head_ref; + while (fast != NULL && fast->next != NULL) { + fast = fast->next->next; + slow = slow->next; + if (slow == fast) return 1; + } + return 0; +} + +void test_checkCircle() { + SinglyLinkedNode* head = NULL; + insertNode(&head, 3); + insertNode(&head, 2); + insertNode(&head, 1); + + int result1 = checkCircle(&head); + printf("has circle: %d\n", result1); + + // make circle linklist + SinglyLinkedNode* current = malloc(sizeof(SinglyLinkedNode)); + current->data = 0; + SinglyLinkedNode* h = current; + for (int i = 1; i < 4; ++i) { + SinglyLinkedNode* node = malloc(sizeof(SinglyLinkedNode)); + node->data = i; + current->next = node; + } + current->next = h; + + int result2 = checkCircle(&h); + printf("has circle: %d\n", result2); +} + +/** 有序链表合并 */ + +void moveNode(SinglyLinkedNode** dest_ref, SinglyLinkedNode** src_ref); + +SinglyLinkedNode* mergeSortedLinkedList(SinglyLinkedNode* la, SinglyLinkedNode* lb) { + // 辅助结点,next 指针持有合并后的有序链表 + SinglyLinkedNode dummy; + + // 有序链表的尾结点 + SinglyLinkedNode* tail = &dummy; + + while (1) { + // 如果有一个链表为空,直接与另一个链表接起来 + if (!la) { + tail->next = lb; + break; + } else if (!lb) { + tail->next = la; + break; + } + + // 将头结点较小的优先添加到 tail + if (la->data <= lb->data) { + moveNode(&(tail->next), &la); + } else { + moveNode(&(tail->next), &lb); + } + tail = tail->next; + } + + return dummy.next; +} + +// 将 src_ref 的头结点,添加到 dest_ref 的头部。 +void moveNode(SinglyLinkedNode** dest_ref, SinglyLinkedNode** src_ref) { + if (*src_ref == NULL) return; + SinglyLinkedNode* new_node = *src_ref; + + *src_ref = new_node->next; + + new_node->next = *dest_ref; + *dest_ref = new_node; +} + +void test_mergeSortedLinkedList() { + SinglyLinkedNode* a = NULL; + insertNode(&a, 10); + insertNode(&a, 5); + insertNode(&a, 0); + + SinglyLinkedNode* b = NULL; + insertNode(&b, 8); + insertNode(&b, 6); + insertNode(&b, 3); + + SinglyLinkedNode* result = mergeSortedLinkedList(a, b); + printLinkedList(result); + + SinglyLinkedNode* result2 = mergeSortedLinkedList(a, NULL); + printLinkedList(result2); +} + +/** 删除倒数第 K 个结点 */ + +void deleteLastKth(SinglyLinkedNode** head_ref, int k) { + if (*head_ref == NULL) return; + + // 快指针向前移动 k-1 + SinglyLinkedNode* fast = *head_ref; + int i = 1; + while (i < k && fast != NULL) { + fast = fast->next; + ++i; + } + + // 如果快指针为空,说明结点个数小于 k + if (fast == NULL) return; + + SinglyLinkedNode* slow = *head_ref; + SinglyLinkedNode* prev = NULL; + while (fast->next != NULL) { + fast = fast->next; + prev = slow; + slow = slow->next; + } + + // 如果 prev 为空,头结点刚好是第 k 个结点 + if (!prev) { + (*head_ref) = (*head_ref)->next; + } else { + prev->next = slow->next; + } + free(slow); +} + +void test_deleteLastKth() { + SinglyLinkedNode* head = NULL; + insertNode(&head, 1); + insertNode(&head, 2); + insertNode(&head, 3); + insertNode(&head, 4); + insertNode(&head, 5); + + // 1. 删除头结点 + deleteLastKth(&head, 5); + printLinkedList(head); + + // 2. 删除中间结点 + deleteLastKth(&head, 2); + printLinkedList(head); + +} + +/** 求中间结点 */ + +SinglyLinkedNode* findMiddleNode(SinglyLinkedNode* head) { + if (!head) return NULL; + SinglyLinkedNode* slow = head; + SinglyLinkedNode* fast = head; + + // 1. 慢指针走一步,快指针两步 + while (fast->next != NULL && fast->next->next != NULL) { + slow = slow->next; + fast = fast->next->next; + } + + return slow; +} + +void test_findMiddleNode() { + SinglyLinkedNode* head = NULL; + insertNode(&head, 1); + insertNode(&head, 2); + insertNode(&head, 3); + insertNode(&head, 4); + insertNode(&head, 5); + + SinglyLinkedNode* middleNode = findMiddleNode(head); + printf("%d\n", middleNode->data); + printLinkedList(head); +} + +/** 工具方法 */ + +// 插入新结点到链表头部 +void insertNode(SinglyLinkedNode** head_ref, int data) { + SinglyLinkedNode* new_node = malloc(sizeof(SinglyLinkedNode)); + new_node->data = data; + new_node->next = *head_ref; + *head_ref = new_node; +} + +// 打印链表 +void printLinkedList(SinglyLinkedNode* node) { + printf("--- start ---\n"); + while (node) { + printf("data: %d\n", node->data); + node = node->next; + } + printf("--- end ---\n"); +} + +// 跑测试 +void test() { + + test_reverse(); + +// test_checkCircle(); + +// test_mergeSortedLinkedList(); + +// test_deleteLastKth(); + +// test_findMiddleNode(); +} From a11f3c1014bd811ce90a0b47ec2636cfb7473653 Mon Sep 17 00:00:00 2001 From: HuaQiang Date: Sun, 7 Oct 2018 17:33:05 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20cpp=20=E5=8D=95?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 cpp 单链表的实现. --- c-cpp/07_linkedlist/SingleList.cpp | 541 +++++++++++++++++++++++++++++ 1 file changed, 541 insertions(+) create mode 100644 c-cpp/07_linkedlist/SingleList.cpp diff --git a/c-cpp/07_linkedlist/SingleList.cpp b/c-cpp/07_linkedlist/SingleList.cpp new file mode 100644 index 00000000..a43079cd --- /dev/null +++ b/c-cpp/07_linkedlist/SingleList.cpp @@ -0,0 +1,541 @@ +#include +#include +#include +using namespace std; + + +class CElement; +/*** + * @brief 单链表容器 +*/ +class CSingleList +{ +public: + CSingleList(); + ~CSingleList(); + + /** + * @brief 插入..链表末尾插入 + * @return 成功返回非空指针,否则失败 + */ + CElement* Insert(void* lpData, int iDataSize); + /** + * @brief 插入..链表指定位置插入 + * @return 成功返回非空指针,否则失败 + */ + CElement* Insert(CElement* lpElement, void* lpData, int iDataSize); + /** + * @brief 删除 + */ + void Delete(CElement*); + + /** + * @brief 链首 + */ + CElement* Begin(); + /** + * @brief 下一个元素 + */ + CElement* Next(); + /*** + * @brief 链尾 + */ + CElement* End(); + + /** + * @brief 是否是空链表 + * @return 空返回TRUE,否则返回FALSE + */ + bool Empty(); + + /** + * @brief 反转 + */ + void Reverse(); + + /** + * @brief 检测环 + * @return 返回TRUE时表示链表存在环,否则不存在环. + */ + bool CheckCircle(); + + /** + * @brief 合并2个有序的链表 + */ + void Merge(CSingleList& lst, std::function); + + /** + * @brief 删除倒数第K个结点 + */ + void DeleteLastKth(int k); + /** + * @brief 求中间节点 + */ + CElement* Center(); +private: + void Insert(CElement* lpNewElement, CElement* lpCurElement, bool bBack = true); + void Insert(CElement* lpNewElement); + CElement* Tail(); + + CSingleList(CSingleList const & rhs); + CSingleList& operator= (CSingleList const& rhs); +private: + /**头结点*/ + CElement* m_lpHead; + /**哨兵*/ + CElement* m_lpSentinel; + /**空结点,用于End()返回 */ + CElement* m_lpNull; + /**当前结点. 枚举时使用. */ + CElement* m_lpCur; +}; + +/*** + * @brief 单链表结点元素. +*/ +class CElement +{ + friend class CSingleList; +protected: + CElement(); + ~CElement(); +public: + /*** + * @brief 获取数据指针 + */ + void* GetDataPtr(); +protected: + /**下一个结点*/ + CElement* m_lpNext; + void* m_lpData; +}; + + +void CreateList(CSingleList& lst) +{ + //循环插入元素到链表尾 + for(int i=1; i<10;i++) + { + int* p = new int(); + *p = i; + lst.Insert(p, 4); + } +} +void PrintList(CSingleList& lst) +{ + CElement* lpElement = lst.Begin(); + while(lpElement != lst.End()) + { + std::cout<<*((int*)lpElement->GetDataPtr())<GetDataPtr()) == 5) + { + int* p = new int(); + *p = 55; + lst.Insert(lpElement,p, 4); + break; + }else{ + lpElement = lst.Next(); + } + } + + std::cout<<"枚举链表当前的元素"<GetDataPtr()) == 7) + { + lst.Delete(lpElement); + break; + }else{ + lpElement = lst.Next(); + } + } + std::cout<<"枚举链表当前的元素"< int{ + if(*((int*)lpT1) < *((int*)lpT2)){ + return -1; + }else if(*((int*)lpT1) == *((int*)lpT2)){ + return 0; + }else if(*((int*)lpT1) > *((int*)lpT2)){ + return 1; + } + return 0; + }); + std::cout<<"合并之后,打印当前链表."<GetDataPtr())<GetDataPtr())<GetDataPtr())<m_lpNext = m_lpSentinel; +} +CSingleList::~CSingleList() +{ + if(NULL != m_lpSentinel) + { + delete m_lpSentinel; + m_lpSentinel = NULL; + } + if(NULL != m_lpNull) + { + delete m_lpNull; + m_lpNull = NULL; + } + if(NULL != m_lpHead) + { + delete m_lpHead; + m_lpHead = NULL; + } +} +CElement* CSingleList::Insert(void* lpData, int iDataSize) +{ + CElement* lpNewElement = new CElement(); + if(NULL == lpNewElement) + { + return NULL; + } + lpNewElement->m_lpData = lpData; + Insert(lpNewElement, Tail()); + return lpNewElement; +} +CElement* CSingleList::Insert(CElement* lpElement, void* lpData, int iDataSize) +{ + if((NULL == lpElement) || (End() == lpElement)) + { + return NULL; + } + CElement* lpNewElement = new CElement(); + if(NULL == lpNewElement) + { + return NULL; + } + lpNewElement->m_lpData = lpData; + Insert(lpNewElement, lpElement); + return lpNewElement; +} +void CSingleList::Insert(CElement* lpNewElement, CElement* lpCurElement, bool bBack /*= true*/) +{ + if(bBack){//插入到指定元素的后面 + lpNewElement->m_lpNext = lpCurElement->m_lpNext; + lpCurElement->m_lpNext = lpNewElement; + }else{//插入到指定元素的前面 + CElement* lpIter = m_lpSentinel; + while(NULL != lpIter) + { + if(lpIter->m_lpNext == lpCurElement) + { + lpNewElement->m_lpNext = lpIter->m_lpNext; + lpIter->m_lpNext = lpNewElement; + break; + }else{ + lpIter = lpIter->m_lpNext; + } + } + } +} + +void CSingleList::Delete(CElement* lpElement) +{ + if((NULL == lpElement) || (End() == lpElement)) + { + return; + } + CElement* lpCurElement = m_lpHead->m_lpNext; + while(NULL != lpCurElement->m_lpNext) + { + if(lpCurElement->m_lpNext == lpElement) + { + lpCurElement->m_lpNext = lpCurElement->m_lpNext->m_lpNext; + break; + }else{ + lpCurElement = lpCurElement->m_lpNext; + } + } +} + +CElement* CSingleList::Tail() +{ + CElement* lpCurElement = m_lpHead->m_lpNext; + while(NULL != lpCurElement->m_lpNext) + { + lpCurElement = lpCurElement->m_lpNext; + } + return lpCurElement; +} + +CElement* CSingleList::Begin() +{ + m_lpCur = NULL; + if(NULL == m_lpHead->m_lpNext->m_lpNext) + { + m_lpCur = End(); + }else{ + m_lpCur = m_lpHead->m_lpNext->m_lpNext; + } + return m_lpCur; +} + +CElement* CSingleList::Next() +{ + if((NULL == m_lpCur) || (End() == m_lpCur)) + { + return m_lpCur; + } + m_lpCur = m_lpCur->m_lpNext; + if(NULL == m_lpCur) + { + m_lpCur = End(); + } + return m_lpCur; +} + +CElement* CSingleList::End() +{ + return m_lpNull; +} + +bool CSingleList::Empty() +{ + return Begin() == End(); +} + +void CSingleList::Reverse() +{ + if(Empty()) + { + return; + } + CElement* lpPre = NULL; + CElement* lpTmp = NULL; + CElement* lpCurElement = m_lpSentinel->m_lpNext; + while(1) + { + lpTmp = lpCurElement->m_lpNext; + lpCurElement->m_lpNext = lpPre; + if(NULL == lpTmp) + { + break; + } + lpPre = lpCurElement; + lpCurElement = lpTmp; + } + m_lpSentinel->m_lpNext = lpCurElement; +} + +bool CSingleList::CheckCircle() +{ + if(Empty()) + { + return false; + } + CElement* lpFast = m_lpSentinel->m_lpNext; + CElement* lpSlow = m_lpSentinel->m_lpNext; + while ((NULL != lpFast) && (NULL != lpFast->m_lpNext)) + { + lpFast = lpFast->m_lpNext->m_lpNext; + lpSlow = lpSlow->m_lpNext; + if (lpFast == lpSlow) + { + return true; + } + } + return false; +} + +void CSingleList::Merge(CSingleList& lst, std::function fnCompare) +{ + CElement* lpL1 = Begin(); + CElement* lpL2 = lst.Begin(); + CElement* lpTail = NULL; + + if(!fnCompare) + { + return; + } + int iRet = 0; + while((lpL2 != lst.End())) + { + if(lpL1 != End()) + { + iRet = fnCompare(lpL1->GetDataPtr(), lpL2->GetDataPtr()); + }else{ + iRet = -1; + } + CElement* lpNewElement = new CElement(); + if(NULL != lpNewElement) + { + lpNewElement->m_lpData = lpL2->GetDataPtr(); + if(lpL1 != End()) + { + Insert(lpNewElement,lpL1, iRet <= 0); + }else{ + if(NULL == lpTail) + { + lpTail = Tail(); + } + Insert(lpNewElement,lpTail); + } + } + lpL2 = lst.Next(); + lpL1 = Next(); + } +} + +void CSingleList::DeleteLastKth(int k) +{ + int i = 1; + if(k <= 0) + { + return; + } + CElement* lpFast = Begin(); + while((lpFast != End()) && (i < k)) + { + lpFast = Next(); + ++i; + } + if (lpFast == End()) + { + return; + } + CElement* lpSlow = Begin(); + CElement* lpPrev = NULL; + while (NULL != lpFast->m_lpNext) + { + lpFast = lpFast->m_lpNext; + lpPrev = lpSlow; + lpSlow = Next(); + } + if(NULL != lpPrev) + { + lpPrev->m_lpNext = lpPrev->m_lpNext->m_lpNext; + } +} + +CElement* CSingleList::Center() +{ + CElement* lpFast = Begin(); + CElement* lpSlow = lpFast; + while((NULL != lpFast->m_lpNext) && (NULL != lpFast->m_lpNext->m_lpNext)) + { + lpFast = lpFast->m_lpNext->m_lpNext; + lpSlow = lpSlow->m_lpNext; + } + return lpSlow; +} + +CElement::CElement() +{ + m_lpNext = NULL; + m_lpData = NULL; +} +CElement::~CElement() +{ + +} + +void* CElement::GetDataPtr() +{ + return m_lpData; +} \ No newline at end of file From 9d6d8d22fb22e1f021636f851595067c824351dd Mon Sep 17 00:00:00 2001 From: Zix <33301611+mvpcaozixiang@users.noreply.github.com> Date: Sun, 7 Oct 2018 21:42:44 +0800 Subject: [PATCH 07/10] Create singleLinkedList.cpp --- c-cpp/06_linkedlist/singleLinkedList.cpp | 229 +++++++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 c-cpp/06_linkedlist/singleLinkedList.cpp diff --git a/c-cpp/06_linkedlist/singleLinkedList.cpp b/c-cpp/06_linkedlist/singleLinkedList.cpp new file mode 100644 index 00000000..680cb126 --- /dev/null +++ b/c-cpp/06_linkedlist/singleLinkedList.cpp @@ -0,0 +1,229 @@ +typedef int DataType; + +//定义 +class SNode +{ +public: + DataType data; + SNode * next; +}; + +class SList +{ +public: + SList(); + SList(int MaxSize); + ~SList(); + void intsertElemAtBegin(DataType x); //头部插入节点 + bool findElem(DataType x); //查找x,存在则返回1,不存在则返回0 + void deleteElemAtEnd(); //删除尾节点 + bool deleteElem(DataType x); //删除指定节点,如果存在则删除,返回1,如果不存在,则删除失败返回0 + bool isEmpty(); // 查看链表是否为空,1表示不为空,0表示为空 + bool isFull(); // 查看链表是否满,1表示不满,0表示满 + void printAll(); + + void * findElemOptim(DataType x); //针对此应用的优化,查找,返回指定元素的前一个节点的指针 + void deleteElemOptim(void * snode); //针对此应用的优化,删除 +private: + int MaxSize; // 链表可以存放最大的数据 + int length; // 链表的长度 + SNode * head; // 指向头节点 + +}; + + +/** + * 1)单链表的插入,删除,查找操作; + * 2)链表中存储的是 int 类型 + * + * Author:caozx + */ +#include +using namespace std; + +// 初始化单链表 +SList::SList(){ + head = new SNode; //申请头节点 + head -> next = NULL; + this -> MaxSize = 10; + this -> length = 0; +} +SList::SList(int MaxSize){ + head = new SNode; //申请头节点 + head -> next = NULL; + this -> MaxSize = MaxSize; + this -> length = 0; +} + +// 销毁单链表,要把开辟的空间都释放,然后再销毁。 +SList::~SList(){ + SNode * ptr, * temp; + ptr = head; + while(ptr -> next != NULL){ + temp = ptr -> next; + ptr -> next = ptr -> next -> next; + delete temp; + } + delete head ; //删除头节点 + this -> head = NULL; + this -> length = 0; +} + +//链表头部插入节点 +void SList::intsertElemAtBegin(DataType x){ + SNode * ptr = new SNode; + ptr -> data = x; + + ptr -> next = head ->next; + head -> next = ptr; + + this -> length ++; +} + +//查找x,存在则返回1,不存在则返回0 + +bool SList::findElem(DataType x) +{ + SNode * ptr; + ptr = head; + while(ptr -> next != NULL){ + if(ptr -> next ->data == x){ + return 1; + } + ptr = ptr -> next; + } + return 0; +} + +// 删除尾结点 +void SList::deleteElemAtEnd(){ + SNode * ptr , * temp; + ptr = head; + while(ptr -> next != NULL && ptr -> next -> next != NULL){ //倒数第二个节点 + ptr = ptr -> next; + } + temp = ptr -> next; + ptr -> next = temp -> next; + this -> length --; + delete temp; +} + +//删除指定节点, +//如果存在则删除,返回1,表示存在且删除成功; +//如果不存在则不删除,返回0,表示不存在该元素,不需要删除,也即删除失败 +bool SList::deleteElem(DataType x) +{ + SNode * ptr, * temp; + ptr = head; + while(ptr -> next != NULL){ + if(ptr -> next ->data == x){ + temp = ptr -> next; + ptr -> next = temp -> next; + delete temp; + this -> length --; + return 1; + } + ptr = ptr -> next; + } + return 0; +} + +// 查看链表是否为空,1表示不为空,0表示为空 +bool SList::isEmpty() +{ + if(this -> length == 0){ //空 + return 0; + } + else{ + return 1; + } +} + +// 查看链表是否满,1表示不满,0表示满 +bool SList::isFull() +{ + if(this -> length == this -> MaxSize){ //满 + return 0; + } + else{ + return 1; + } +} + +// 打印 +void SList::printAll() +{ + SNode * ptr; + ptr = head; + while(ptr -> next != NULL){ + ptr = ptr -> next; + cout << ptr-> data <<" "; + } + cout << endl; +} + +//针对此应用的优化,查找, +//若存在则返回指定元素的前一个节点的指针 +//若不存在,则返回NULL + +void * SList::findElemOptim(DataType x) +{ + SNode * ptr; + ptr = head; + while(ptr -> next != NULL){ + if(ptr -> next ->data == x){ + return (void *)ptr; + } + ptr = ptr -> next; + } + return NULL; +} + +//针对此应用的优化,删除 + +void SList::deleteElemOptim(void * snode) +{ + SNode * ptr, * temp; + ptr = (SNode *)snode; + temp = ptr -> next; + ptr -> next = temp -> next; + this -> length --; + delete temp; +} +int main(int argc, char const *argv[]) +{ + cout << "test "<< endl; + SList slist(10); //缓存最大10个。 + int num = 0; + while(1) + { + cout << "please enter a number,99999== exit" << endl; + cin >> num; + if(num == 99999) + break; + /* 未优化 + if(slist.findElem(num)){ //存在 + slist.deleteElem(num); //把原来的位置删除 + slist.intsertElemAtBegin(num); //在链表头插入 + } + */ + //优化 + SNode * prePtr = (SNode *)slist.findElemOptim(num); + if(prePtr != NULL){ //存在 + slist.deleteElemOptim(prePtr); //把原来的位置删除 + slist.intsertElemAtBegin(num); //在链表头插入 + } + else{ //不存在 + if(slist.isFull()){ //不满 + slist.intsertElemAtBegin(num); + } + else{ //满 + slist.deleteElemAtEnd(); + slist.intsertElemAtBegin(num); + } + } + slist.printAll(); + } + return 0; + system("pause"); +} From 5ce1e246dfaf1d2a6405b0dbd3c51f57e4b949eb Mon Sep 17 00:00:00 2001 From: SuperChenSSS Date: Mon, 8 Oct 2018 00:20:58 +0800 Subject: [PATCH 08/10] Fix bugs: single_list.c has some grammer errors.Fixed. --- c-cpp/06_linkedlist/single_list.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/c-cpp/06_linkedlist/single_list.c b/c-cpp/06_linkedlist/single_list.c index 0fd032aa..6c12868e 100644 --- a/c-cpp/06_linkedlist/single_list.c +++ b/c-cpp/06_linkedlist/single_list.c @@ -41,7 +41,7 @@ void insert_head(struct single_list_head *head, struct single_list *elem) insert(&head->head, elem); } -struct single_list* delete(struct single_list **prev) +struct single_list* del(struct single_list **prev) { struct single_list *tmp; @@ -53,12 +53,12 @@ struct single_list* delete(struct single_list **prev) tmp->next = NULL; return tmp; -} +}; struct single_list* delete_head(struct single_list_head* head) { - return delete(&head->head); -} + return del(&head->head); +}; struct single_list** search(struct single_list_head* head, int target) { @@ -70,7 +70,7 @@ struct single_list** search(struct single_list_head* head, int target) ; return prev; -} +}; void reverse(struct single_list_head* head) { @@ -117,7 +117,7 @@ struct single_list* middle(struct single_list_head* head) } return NULL; -} +}; int main() { @@ -153,7 +153,7 @@ int main() else printf("The list not contains 2\n"); - delete(prev); + del(prev); prev = search(&head, 2); printf("After remove 2\n"); if ((*prev) && ((*prev)->val == 2)) @@ -173,4 +173,3 @@ int main() return 0; } - From 5712d7f6283ac7577e667381c31689ed0bdcdca8 Mon Sep 17 00:00:00 2001 From: Smallfly Date: Mon, 8 Oct 2018 08:13:39 +0800 Subject: [PATCH 09/10] add base check --- c-cpp/07_linkedlist/LinkedListAlgo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c-cpp/07_linkedlist/LinkedListAlgo.c b/c-cpp/07_linkedlist/LinkedListAlgo.c index e35d612a..159522fd 100644 --- a/c-cpp/07_linkedlist/LinkedListAlgo.c +++ b/c-cpp/07_linkedlist/LinkedListAlgo.c @@ -151,7 +151,7 @@ void test_mergeSortedLinkedList() { /** 删除倒数第 K 个结点 */ void deleteLastKth(SinglyLinkedNode** head_ref, int k) { - if (*head_ref == NULL) return; + if (*head_ref == NULL || k == 0) return; // 快指针向前移动 k-1 SinglyLinkedNode* fast = *head_ref; From 2d6849bc35e99d4b83a1380705955d3eff6be74e Mon Sep 17 00:00:00 2001 From: Zix <33301611+mvpcaozixiang@users.noreply.github.com> Date: Mon, 8 Oct 2018 11:16:15 +0800 Subject: [PATCH 10/10] Rename singleLinkedList.cpp to LRUBasedLinkedList.cpp --- .../{singleLinkedList.cpp => LRUBasedLinkedList.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename c-cpp/06_linkedlist/{singleLinkedList.cpp => LRUBasedLinkedList.cpp} (100%) diff --git a/c-cpp/06_linkedlist/singleLinkedList.cpp b/c-cpp/06_linkedlist/LRUBasedLinkedList.cpp similarity index 100% rename from c-cpp/06_linkedlist/singleLinkedList.cpp rename to c-cpp/06_linkedlist/LRUBasedLinkedList.cpp