Skip to content

feat: linked-list add reversal method, close #6 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions src/linked-list/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ describe('LinkedList class', () => {
linkedList.append(2).append(3)
expect(linkedList.toString()).toBe('1,2,3')

const stringHandler = (value: any) => value;
expect(linkedList.toString(stringHandler)).toBe('1,2,3');
const stringHandler = (value: any) => value
expect(linkedList.toString(stringHandler)).toBe('1,2,3')
})

it('Method toArray()', () => {
Expand Down Expand Up @@ -150,8 +150,8 @@ describe('LinkedList class', () => {
expect(linkedList.delete).toBeInstanceOf(Function)
})

it("should return null if the list is empty()", () => {
expect(linkedList.delete(1)).toBe(null);
it('should return null if the list is empty()', () => {
expect(linkedList.delete(1)).toBe(null)
})

it('should delete nothing if not found value', () => {
Expand Down Expand Up @@ -212,18 +212,16 @@ describe('LinkedList class', () => {
expect((linkedList.tail as Node).value).toBe(5)
})

it("should set head & tail correctly when list have only one node", () => {
it('should set head & tail correctly when list have only one node', () => {
linkedList.append(1).deleteHead()

expect(linkedList.head).toBe(null)
expect(linkedList.tail).toBe(null)
})

it("should return null if the list is empty()", () => {
expect(linkedList.deleteHead()).toBe(null);
it('should return null if the list is empty()', () => {
expect(linkedList.deleteHead()).toBe(null)
})


})

describe('Method deleteTail()', () => {
Expand All @@ -245,11 +243,11 @@ describe('LinkedList class', () => {
expect((linkedList.tail as Node).value).toBe(4)
})

it("should return null when list is empty", () => {
expect(linkedList.deleteTail()).toBe(null);
it('should return null when list is empty', () => {
expect(linkedList.deleteTail()).toBe(null)
linkedList.append(1)
linkedList.append(2)
expect((linkedList.deleteHead() as Node).value).toBe(1);
expect((linkedList.deleteHead() as Node).value).toBe(1)
})
})

Expand All @@ -272,7 +270,6 @@ describe('LinkedList class', () => {
})

describe('Method insert()', () => {

it('should insert value after specified value by default', () => {
linkedList
.append(1)
Expand Down Expand Up @@ -335,16 +332,40 @@ describe('LinkedList class', () => {
expect(linkedList.toString()).toBe('1,2,3')
})

it("should do nothing when list empty", () => {
it('should do nothing when list empty', () => {
linkedList.insert(5, 3)
expect(linkedList.toString()).toBe('')
expect(linkedList.head) .toBe(null)
expect(linkedList.head).toBe(null)
expect(linkedList.tail).toBe(null)

linkedList.insert(5, 3, "before")
linkedList.insert(5, 3, 'before')
expect(linkedList.toString()).toBe('')
expect(linkedList.head) .toBe(null)
expect(linkedList.head).toBe(null)
expect(linkedList.tail).toBe(null)
})
})

describe('Method reversal()', () => {
it('should reversal list', () => {
linkedList
.append(1)
.append(2)
.append(3)
const reversaledList = linkedList.reversal()
expect(reversaledList).toBeDefined()
expect(reversaledList).toBeInstanceOf(LinkedList)
expect(reversaledList.toString()).toBe('3,2,1')
expect((reversaledList.head as Node).value).toBe(3)
expect((reversaledList.tail as Node).value).toBe(1)
})

it('should do nothing when list is empty', () => {
const reversaledList = linkedList.reversal()
expect(reversaledList).toBeDefined()
expect(reversaledList).toBeInstanceOf(LinkedList)
expect(reversaledList.toString()).toBe('')
expect(reversaledList.head).toBe(null)
expect(reversaledList.tail).toBe(null)
})
})
})
25 changes: 23 additions & 2 deletions src/linked-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class LinkedList {
this.head = null
this.tail = null
} else {
this.head = headNode.next;
this.head = headNode.next
}
}

Expand Down Expand Up @@ -212,6 +212,27 @@ export class LinkedList {
return deletedNode
}

/**
* 反转链表
*/
public reversal(): LinkedList {
let currentNode = this.head
let prevNode = null
let nextNode = null

while (currentNode) {
nextNode = currentNode.next
currentNode.next = prevNode
prevNode = currentNode
currentNode = nextNode
}

this.tail = this.head
this.head = prevNode

return this
}

/**
* 查询链表是否为空
*/
Expand All @@ -231,7 +252,7 @@ export class LinkedList {
return result
}

public toString( handler: (value: any) => string = stringifyDefault ): string {
public toString(handler: (value: any) => string = stringifyDefault): string {
return this.toArray()
.map(value => {
return handler(value)
Expand Down